Thursday, 5 September 2013

Using batch files to copy all files from a directory tree to a single directory

Using batch files to copy all files from a directory tree to a single
directory

I needed to copy all the files from a directory tree into a single
directory. A quick search provided me with this method:
for /f "tokens=*" %a in ('dir /b /s /a-d') do @copy "%a" "c:\Single-Folder"
I tried it, and it worked just fine. Deciding to simplify things a bit, I
created a quick batch file so I didn't have to look this up in the future.
my batch file looks like this:
set COPY_FROM="C:\Users\me\Desktop\Disc 1"
set COPY_TO="C:\Testing\test"
cd %COPY_FROM%
for /f "tokens=*" %a in ('dir /b /s /a-d') do @copy %COPY_TO%
pause
Unfortunately, when I execute this, I get the error:
C:\Users\me\Desktop\Tools>set COPY_FROM="C:\Users\me\Desktop\Disc 1"
C:\Users\me\Desktop\Tools>set COPY_TO="C:\Testing\test"
"\Users\me\Desktop\Disc 1"') was unexpected at this time.
C:\Users\me\Desktop\Tools>for /f "tokens=*" "\Users\me\Desktop\Disc 1"')
do @copy "\Testing\test"
What works if I enter it into the command line does not work when run as a
batch file. I did try replacing the variables with the actual path, but
got the same error. Even when I create a batch file with only the line
that works from the command line, it doesn't work when running from the
file. Does anyone know what I am doing wrong?
Thank you in advance for any assistance.

No comments:

Post a Comment