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
NancyCPSAsked:
Who is Participating?
 
knightEknightCommented:
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
 
NancyCPSAuthor 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
 
knightEknightCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
NancyCPSAuthor Commented:
Thanks!!!

what is the /d switch after CD?
0
 
knightEknightCommented:
/d changes the drive as well as the current directory.  It is the same as doing this:

E:
CD  E:\vol2\folder1\myfolder
0
 
NancyCPSAuthor Commented:
Ahhh great!  Thanks so much.
0
 
knightEknightCommented:
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.

All Courses

From novice to tech pro — start learning today.