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?

Trashy
  • 3
  • 2

2 Answers2

1

To avoid confusion for me, I change the "origin" to "source". E.g. set "origin=%nameor%" changed to set "source=%nameor%".

To print out "C:\folder1" to "C:\foldern", you should use echo !%source%!, else you will just see "source1" to "sourcen".

Happy Face
  • 1,061
  • 7
  • 18
0

Your problem is just about array element management. Try this:

@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!
)

The first part is equivalent to your series of individual element assignments. In this way is easier to add new pairs of values.

For further description on array management in Batch files, see: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108