# Copy commands work in command prompt but not in BAT or CMD file

When I use the following lines from a command prompt to copy files from subfolders into one folder it works just fine.  However, when I enter them into a BAT or CMD file, they no longer work.  What am I missing?

E:\vol2\folder1\myfolder
for /f "tokens=*" %a in ('dir /b /s /a-d') do @copy "%a" "P:\folder1\myfolder2\234567" /y
LVL 1
###### Who is Participating?

Commented:
In .bat and .cmd files, you need to double the percent symbols:

for /f "tokens=*" %%a in ('dir /b /s /a-d') do @copy "%%a" "P:\folder1\myfolder2\234567" /y
0

Author Commented:
Now the batch file is working - however, it copies the entire drive (E:\)  instead of copying the folders that I want (E:\vol2\folder1\myfolder).  Imagine my surprise when I had over 4000 files instead of 400.
0

Commented:
That's because in the first line you have this:

E:\vol2\folder1\myfolder

but I think you meant to have this:

CD/d  E:\vol2\folder1\myfolder
0

Author Commented:
Thanks!!!

what is the /d switch after CD?
0

Commented:
/d changes the drive as well as the current directory.  It is the same as doing this:

E:
CD  E:\vol2\folder1\myfolder
0

Author Commented:
Ahhh great!  Thanks so much.
0

Commented:
Glad to help where I can.  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.