Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-03
7
Medium Priority
?
644 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:NancyCPS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 35029237
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
 
LVL 1

Author Comment

by:NancyCPS
ID: 35036951
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
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 2000 total points
ID: 35037062
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 1

Author Comment

by:NancyCPS
ID: 35037619
Thanks!!!

what is the /d switch after CD?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35037631
/d changes the drive as well as the current directory.  It is the same as doing this:

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

Author Comment

by:NancyCPS
ID: 35037643
Ahhh great!  Thanks so much.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35037822
Glad to help where I can.  :)
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question