Monday 1 July 2019

xcopy - copy array in a batch file


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.



Answer



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



No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...