parameters in a DOS .bat file

Posted on 2003-03-10
Medium Priority
Last Modified: 2011-09-20
When I execute the following at the DOS command line, I have no problem:

for /f %k in ('dir/b/s *.xml') do if exist Z:%~pk*.* xcopy %k z:%~pnxk

However, when I put this in a DOS .bat file, it dies on starting at %k. The command line ends up looking like:

for /f ~pk*.* xcopy ~pnxk

and I get this error: xcopy was unexpected at this time.

I suspect this is because the .bat expects a command line parameter in %k, correct? Does anyone know how I can fix this? This a great command provided by billious, but I need to get it working in a batch file.
Question by:tpatten
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
  • 5
LVL 30

Expert Comment

ID: 8103568
Inside of a batch file use double quotes in for statement.

for /f %%k in ('dir/b/s *.xml') do if exist Z:%%~pk*.* xcopy %%k z:%%~pnxk

Good Luck,
LVL 30

Expert Comment

ID: 8103573
double quotes = double parans :)

Accepted Solution

billious earned 120 total points
ID: 8111550
One more time, a little more slowly, SteveGTR

The PERCENTAGE SIGNS need to be doubled before the for-loop control letter if the for-loop is contained in a batch file.

...as mentioned in the last few comments (esp. from pbarette) in



Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

LVL 30

Expert Comment

ID: 8111610
Yikes Bill! Thanks :)
LVL 30

Expert Comment

ID: 9624731
My answer with the aid of Bill did the trick. Maybe split the points?

Author Comment

ID: 9661825
This was answered nicely by nillious in the thread he linked to: http://www.experts-exchange.com/Operating_Systems/MSDOS/Q_20543456.html

Unfortunately I posted this question in two different places. The other thread turned out to be the one that took off.
LVL 30

Expert Comment

ID: 9661882
These questions may be related to the same task that you were performing, but they are not the same question...

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 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