2

I have several files located across several directories/subfolders that I need to copy to several destinations. I currently do this using xcopy but I do it line by line, so there are several lines of code. A friend suggested that I use an array instead which is what I'm trying to do now.

I've tried several variations of getting this to work. My last variation looks like this:

@echo off
set "mansource=somelocation\location1\sourcefolder"
set "mandest=someloacation\location2\destfolder

set src[0]="%MANSOURCE%\folderA\number1.pdf"
set dest[0]="%MANDEST%\folderB\number\1"

set src[1]="%MANSOURCE%\folderC\number2.pdf"
set dest[1]="%MANDEST%\folderD\number\2"
set /a "x=0"

for /r "%mansource%" %%f in (!src!) do xcopy "%%f" "!dest!" /v/y 
pause

So I know this is wrong since it doesn't work, but nothing else I've tried has worked either. Every example I've found online so far about using arrays in a batch only has a "do ( echo" command to either list what is being indexed or print a statement to the cmd prompt. I haven't been able to find any examples concerning an actual function like copying. I did have a variation of the above code but it just resulted an infinite loop with "0 files copied" repeating forever.

The idea is to have the file in src[0] copied to the locations in dest[0], then the file in src[1] and so on.

Any advice would be appreciated.

Note: Using a *.pdf wildcard will not work as there are other pdf's in these locations and I only need specific ones to be copied. Also, each file is in a different location from the next and each copy destination for that file is also different.

2
  • 1
    First thing that sticks out, you are missing a ending quote on line 3; is that a typo here, or in the actual code? Apr 25, 2017 at 15:39
  • It's a typo here. Apr 25, 2017 at 16:33

2 Answers 2

0

Since you are looking for a batch solution, another way to go about this is to create a FOR loop with the 0 thru x index range. The FOR loop will iterate the index number values and use the CALL command to call correlated subroutine names.

The subroutine label names will all be the same except the index number values (as in the FOR loop) are appended to the label names (i.e. :idx0, :idx1, :idx2, and so on).

Each subroutine should have the "same" variable names within but the values of each should be set accordingly for the need, but each subroutine must end with GOTO :EOF. The GOTO :EOF will pass control back to the caller and allow it to finish out it's logic so in this case the FOR /R loop will start.

Be sure to that once your logic ends, that you have it EXIT or GOTO :EOF so it does not move onto the logic beneath such as subroutine labels intended only to be executed with CALL commands.

Example Script

@echo off

set "mansource=somelocation\location1\sourcefolder"
set "mandest=someloacation\location2\destfolder"

FOR %%A IN (0,1) DO CALL :idx%%A
for /r "%mansource%" %%f in (!src!) do xcopy "%%f" "!dest!" /v/y 
pause
::GOTO :EOF
EXIT

:idx0
set src="%MANSOURCE%\folderA\number1.pdf"
set dest="%MANDEST%\folderB\number\1"
GOTO :EOF

:idx1
set src="%MANSOURCE%\folderC\number2.pdf"
set dest="%MANDEST%\folderD\number\2"
GOTO :EOF

Further Resources

0

Batch + "arrays" (which are not actually arrays) are a pain. :)

I'd suggest using PowerShell instead, since it supports proper arrays. Something like:

$mansource = "somelocation\location1\sourcefolder"
$mandest = "someloacation\location2\destfolder"

# Create empty arrays
$sources = @()
$destinations = @()

# Add values to arrays
$sources += Join-Path $mansource "\folderA\number1.pdf"
$sources += Join-Path $mansource "\folderC\number2.pdf"

$destinations += Join-Path $mandest "\folderB\number\1"
$destinations += Join-Path $mandest "\folderD\number\2"

# Iterate over the Sources array and copy to the matching Destination.
for ($i = 0; $i -le $sources.length; $i++) {
    Copy-Item $sources[$i] $destinations[$i] -Force
}
1
  • Yeah I thought of using powershell but I'd rather use batch for this particular purpose if I can. Apr 25, 2017 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .