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

x
?
Solved

Loop in a batch file, and increment a UNC path

Posted on 2006-06-14
11
Medium Priority
?
6,969 Views
Last Modified: 2011-08-18
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
Comment
Question by:budchawla
[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
  • 6
  • 3
  • 2
11 Comments
 
LVL 4

Assisted Solution

by:Nat_c
Nat_c earned 500 total points
ID: 16902304
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
 
LVL 10

Author Comment

by:budchawla
ID: 16902363
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
 
LVL 10

Author Comment

by:budchawla
ID: 16902534
funny thing, if I run the command itself from the prompt, no problem...
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 10

Author Comment

by:budchawla
ID: 16902549
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
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 16902569
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
 
LVL 10

Author Comment

by:budchawla
ID: 16902583
Also, do you know of a good syntax reference for this kind of thing?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 16902585
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
 
LVL 10

Author Comment

by:budchawla
ID: 16902588
aha .. that's it, thanks SteveGTR...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 16902600
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
 
LVL 4

Expert Comment

by:Nat_c
ID: 16902611
You can type "help for" at the command prompt and it will tell you all the syntax
0
 
LVL 10

Author Comment

by:budchawla
ID: 16902644
thanks guys.. both resources useful!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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