• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6987
  • Last Modified:

Loop in a batch file, and increment a UNC path

Hi ..
I need to copy a file from my server onto 50 computers, each one is named computer-01, computer-02... computer-50.
How can I do this via a batch file without copy/pasting the line 50 times?!

copy \\server\file.xyz \\computer-01\c$\progra~1\whateverfolder\

I'm sure this is quite simple, but I haven't got a massive amount of experience with batch file scripting!

Thanks!
0
budchawla
Asked:
budchawla
  • 6
  • 3
  • 2
2 Solutions
 
Nat_cCommented:
Hi budchawla,

You can use the "for" to repeat a command for multiple variables

You can use this as a test to prove the principle

for /l %n in (1,1,3) do md dir%n

%n is the variable
(start number,increment,end number)
do "command"

The above will create 3 directories call dir1, dir2, dir3

Your commands would need to be

for /l %n in (1,1,9) do copy \\server\file.xyz \\computer-0%n\c$\progra~1\whateverfolder
for /l %n in (10,1,50) do copy \\server\file.xyz \\computer-%n\c$\progra~1\whateverfolder

I think you will need to do this in the two commands as your question says the PC names have leading zeros, the 1st command will do 01 - 09 (notice the 0 before %n) the next command will do 10 - 50.

Hope this all makes sense.
Nat
0
 
budchawlaAuthor Commented:
Hi Nat,

All makes sense to me.. but I just tried it by creating a batch file in the root of c: with the following:
for /l %n in (1,1,8) do md dir%n
exactly as you said, and I get a message saying the syntax of the command is incorrect?
0
 
budchawlaAuthor Commented:
funny thing, if I run the command itself from the prompt, no problem...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
budchawlaAuthor Commented:
aha... if I type the whole filename at the prompt, (type in md.bat rather than just md) I get a different message:

n was unexpected at this time.

Any ideas?
0
 
SteveGTRCommented:
When you use the for processing in a batch file you must use double quotes like so:

for /l %%n in (1,1,8) do md dir %%n

Good Luck,
Steve
0
 
budchawlaAuthor Commented:
Also, do you know of a good syntax reference for this kind of thing?
0
 
SteveGTRCommented:
Sorry inserted extra space in your md command:

for /l %%n in (1,1,8) do if not exist "dir%%n" md dir%%n

Added a check to see if the directory exists to make up for my mistake :)
0
 
budchawlaAuthor Commented:
aha .. that's it, thanks SteveGTR...
0
 
SteveGTRCommented:
You can type /? for any command on the command prompt. For example:

for /?

or

if /?

I use that all the time. Also, this site has a bunch of good information on batch processing:

http://www.robvanderwoude.com/index.html
0
 
Nat_cCommented:
You can type "help for" at the command prompt and it will tell you all the syntax
0
 
budchawlaAuthor Commented:
thanks guys.. both resources useful!
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now