0

In my batch I want to copy a variable amount of source- to target destinations.

I want to define like this:

@setlocal EnableDelayedExpansion

set source1="C:\folder1"
set target1="f:\folder1"

set source2="C:\folder2" 
set target2="f:\folder2"  
...

set sourcen="C:\foldern" 
set targetn="f:\foldern"

Dependently from a defined amount of folders

set numFolder=5

I want to go through the folders in a loop:

set /a COUNT=0

:LOOP
echo %COUNT%
set /a COUNT+=1

rem write the NAME of the parameter variable (source1,source2 etc.) in nameor 
set "nameor=source%COUNT%"
rem write the VALUE of the parameter variable (source1,source2 etc.) into origin ("C:\folder1", "C:\folder2")
set "origin=%nameor%"
echo %origin%

if %COUNT% lss %numFolder% goto LOOP

When I show

echo %nameor%

I get what I expectet: source1, source2 etc. but

echo %%%origin%%%

only provides

source1

instead of the expected value

"C:\folder1"

I thought, that I could resolve this by using DelayedExpansion but what did I miss?

4

2 回答 2

1

为了避免混淆,我将“来源”更改为“来源”。例如set "origin=%nameor%"更改为set "source=%nameor%".

要将“C:\folder1”打印到“C:\foldern”,您应该使用echo !%source%!,否则您只会看到“source1”到“sourcen”。

于 2015-08-06T10:42:39.423 回答
0

您的问题只是关于数组元素管理。尝试这个:

@echo off
setlocal EnableDelayedExpansion

rem Define the two arrays
set i=0
for %%a in ("C:\folder1=f:\folder1"
            "C:\folder2=f:\folder2"
            "C:\foldern=f:\foldern") do (
   set /A i+=1
   for /F "tokens=1,2 delims==" %%b in (%%a) do (
      set source!i!="%%a"
      set target!i!="%%b"
   )
)

rem Show up to numFolder elements of both arrays
set numFolder=5
for /L %%i in (1,1,%numFolder%) do (
   echo %%i- Source%%i=!source%%i!,  Target%%i=!target%%i!
)

第一部分相当于您的一系列单个元素分配。这样更容易添加新的值对。

有关批处理文件中数组管理的更多说明,请参见:cmd.exe(批处理)脚本中的数组、链表和其他数据结构

于 2015-08-06T18:49:54.200 回答