Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to run the same command many times with different path?

29 views
Skip to first unread message

Alex Blekhman

unread,
Mar 7, 2002, 9:36:22 AM3/7/02
to
Hi,

I need to run the same command for several computers connected to
network. Computer name pattern looks like that: CompXXX, where XXX is
numbers: 001, 002, 003... For now I just do copy&paste in batch file:

copy MyFile.txt \\Comp001\C\MyFile.txt
copy MyFile.txt \\Comp002\C\MyFile.txt
copy MyFile.txt \\Comp003\C\MyFile.txt
...

Can I do something more clever than that? Something with 'for' command
maybe? I'm running it under W2K.

Thanks in advance
Alex


Joe Rookie

unread,
Mar 7, 2002, 10:03:30 AM3/7/02
to
This is what I do:

CALL OTHERFILE.CMD Server1
CALL OTHERFILE.CMD Server2
CALL OTHERFILE.CMD Server3

And the OTHERFILE.CMD file has all the actions I want to perform (works
great when you have 10-20 things to do on each server ... It looks, though,
like you want to do one command to all PCs/Servers in your network ... I'm
sure someone with more command file expertise than I can come up with
something better ...

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:eOFKiVexBHA.1892@tkmsftngp03...

Neal Emminger

unread,
Mar 7, 2002, 10:23:32 AM3/7/02
to
Sure - how about something like this:

for /f %%i in ('netview /ntw /domain:DOMAINNAME') do copy MyFile.txt
%%i\C\MyFile.txt

for all of the workstations in the domain

or maybe

for /f %%i in ('type c:\wslist.txt') do copy MyFile.txt %%i\C\MyFile.txt
obviously, you'd have the list in c:\wslist.txt here. . . .

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:eOFKiVexBHA.1892@tkmsftngp03...

Alex Blekhman

unread,
Mar 7, 2002, 11:43:15 AM3/7/02
to
"Joe Rookie" <spam....@no-email.com> wrote in message
news:eZcKqkexBHA.1652@tkmsftngp04...

> This is what I do:
>
> CALL OTHERFILE.CMD Server1
> CALL OTHERFILE.CMD Server2
> CALL OTHERFILE.CMD Server3
>
> And the OTHERFILE.CMD file has all the actions I want to perform
(works
> great when you have 10-20 things to do on each server ... It looks,
though,
> like you want to do one command to all PCs/Servers in your network
... I'm
> sure someone with more command file expertise than I can come up
with
> something better ...
>

Yes, I'm doing similar thing, but it can be large number of machines
(up to 96, actually), so I need something where I just put numbers in
one command. Like 'for' for instance.


Alex Blekhman

unread,
Mar 7, 2002, 11:45:19 AM3/7/02
to
"Neal Emminger" <emmin...@aetna.com> wrote in message
news:#vpU3vexBHA.2588@tkmsftngp07...

> Sure - how about something like this:
>
> for /f %%i in ('netview /ntw /domain:DOMAINNAME') do copy MyFile.txt
> %%i\C\MyFile.txt
>
> for all of the workstations in the domain

This is not appropriate for my task since there are other computers in
the network. Not only CompXXX.

>
> or maybe
>
> for /f %%i in ('type c:\wslist.txt') do copy MyFile.txt
%%i\C\MyFile.txt
> obviously, you'd have the list in c:\wslist.txt here. . . .

Hm.. This looks very promising I'll try it definitely.

Thanks


Clay Calvert

unread,
Mar 7, 2002, 7:13:33 PM3/7/02
to
On Thu, 7 Mar 2002 10:23:32 -0500, "Neal Emminger"
<emmin...@aetna.com> wrote:

>Sure - how about something like this:
>
>for /f %%i in ('netview /ntw /domain:DOMAINNAME') do copy MyFile.txt
>%%i\C\MyFile.txt

You're using a freeware tool from Marty List, which is fine. If you
want to use this same technique with built in tools then use:

for /f %%i in ('netview /domain:DOMAINNAME ^| find "\\"') do copy
MyFile.txt %%i\C$\MyFile.txt


>for all of the workstations in the domain
>
>or maybe
>
>for /f %%i in ('type c:\wslist.txt') do copy MyFile.txt %%i\C\MyFile.txt
>obviously, you'd have the list in c:\wslist.txt here. . . .

In both examples, don't you need a "$" after the "C", because it is a
hidden share?

>"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
>news:eOFKiVexBHA.1892@tkmsftngp03...
>> Hi,
>>
>> I need to run the same command for several computers connected to
>> network. Computer name pattern looks like that: CompXXX, where XXX is
>> numbers: 001, 002, 003... For now I just do copy&paste in batch file:
>>
>> copy MyFile.txt \\Comp001\C\MyFile.txt
>> copy MyFile.txt \\Comp002\C\MyFile.txt
>> copy MyFile.txt \\Comp003\C\MyFile.txt
>> ...
>>
>> Can I do something more clever than that? Something with 'for' command
>> maybe? I'm running it under W2K.
>>
>> Thanks in advance
>> Alex
>>
>>
>

Clay Calvert
Replace "W" with "L" in email.

Clay Calvert

unread,
Mar 7, 2002, 7:24:31 PM3/7/02
to

If you are running contiguous numbers then you can use:

for /L %%a in (1,1,9) do copy MyFile.txt \\Comp00%%a\C$\MyFile.txt

Unfortunately, you normally would have to add another line for 10 to
99, and then another for 100 to 999... if you're going that high.

for /L %%a in (10,1,99) do copy MyFile.txt \\Comp0%%a\C$\MyFile.txt
for /L %%a in (100,1,999) do copy MyFile.txt \\Comp%%a\C$\MyFile.txt


With Win2k, you can use this routine to work around that.

for /L %%a in (1,1,120) do call:Three %%a
goto:eof
:Three
set Three=00%%a
copy MyFile.txt \\Comp%Three:~-3%\C$\MyFile.txt

Clay Calvert

unread,
Mar 7, 2002, 7:27:10 PM3/7/02
to
On Thu, 07 Mar 2002 19:13:33 -0500, Clay Calvert
<ccal...@Wanguru.com> wrote:

>In both examples, don't you need a "$" after the "C", because it is a
>hidden share?

Sorry, I noticed you got this from the OP's post.

Cheers,

Dean Wells

unread,
Mar 7, 2002, 9:50:54 PM3/7/02
to
Amazing that one single space can make it all sound so wrong :)

--
Dean Wells [MVP]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]

"Clay Calvert" <ccal...@Wanguru.com> wrote in message
news:f80g8u8rogns275ri...@4ax.com...
: On Thu, 7 Mar 2002 10:23:32 -0500, "Neal Emminger"

Al Dunbar

unread,
Mar 9, 2002, 1:39:25 PM3/9/02
to

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:u42xldfxBHA.2104@tkmsftngp02...

> "Neal Emminger" <emmin...@aetna.com> wrote in message
> news:#vpU3vexBHA.2588@tkmsftngp07...
> > Sure - how about something like this:
> >
> > for /f %%i in ('netview /ntw /domain:DOMAINNAME') do copy MyFile.txt
> > %%i\C\MyFile.txt
> >
> > for all of the workstations in the domain
>
> This is not appropriate for my task since there are other computers in
> the network. Not only CompXXX.

Still, it *might* be the basis of a good approach if the number of computers
that are NOT to receive copies of the file are not in a very large majority,
with something like:

for /f %%i in ('netview /ntw /domain:DOMAINNAME') do if exist
%%i\C\COPYTARGET.TXT copy MyFile.txt %%i\C\MyFile.txt

Then, you just need to ensure that the COPYTARGET.TXT file exists in only
those computers that need to participate. This simplifies things when, for
example, the set of computers needing this file changes for any reason, as
you will not need to modify the batch file.

Alternately, if the participating computers can be determined simply by
their names, the above approach would not require touching all of the
computers in the domain.

/Al

Clay Calvert

unread,
Mar 9, 2002, 2:59:19 PM3/9/02
to
On Thu, 7 Mar 2002 18:45:19 +0200, "Alex Blekhman"
<tkfx....@yahoo.com> wrote:

>"Neal Emminger" <emmin...@aetna.com> wrote in message
>news:#vpU3vexBHA.2588@tkmsftngp07...
>> Sure - how about something like this:
>>
>> for /f %%i in ('netview /ntw /domain:DOMAINNAME') do copy MyFile.txt
>> %%i\C\MyFile.txt
>>
>> for all of the workstations in the domain
>
>This is not appropriate for my task since there are other computers in
>the network. Not only CompXXX.

This will only process computers with names starting with "COMP".

for /f %%i in ('net view ^| find "\\COMP"') do (
copy MyFile.txt %%i\C$\MyFile.txt)

I use the above to differentiate between computer 'types' based on
naming convention.

As I'm sure you know, the /domain:DOMAINNAME is only needed if running
the script against a different domain than what you are currently
logged on to.

Alex Blekhman

unread,
Mar 10, 2002, 1:50:04 AM3/10/02
to
"Clay Calvert" <ccal...@Wanguru.com> wrote in message
news:lh0g8ugk3176m3t9h...@4ax.com...
^^^
Did you mean %1 instead?

> copy MyFile.txt \\Comp%Three:~-3%\C$\MyFile.txt
>

Thanks a lot. That was exactly what I need.

And some OT question. Where did you get all this esoteric wisdom? The
help provided with W2K is rather laconic about batch scripts.
Definitely I didn't find there "%VarName:~-3%" trick.

Thanks in advance
Alex


Alex K. Angelopoulos

unread,
Mar 10, 2002, 2:57:52 AM3/10/02
to
Why don't you know? Clay was BORN in a command prompt!

Once you're "committed" to a command prompt solution for things, you start digging really hard, asking people, doing Internet searches, etc. Much of the information is actually there in documentation on your PC - just very hard to pull out; you can get some of it by trying /? on everything in sight.

But I would never have pulled that usage out of MY hat...

A really good source for some details on using advanced stuff like that is Tim Hill's book "Windows NT Shell Scripting". Or you can learn the way I do - come in here and ask someone like Clay or Dean or Al or... :)

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message news:uPib8#$xBHA.1912@tkmsftngp02...
: "Clay Calvert" <ccal...@Wanguru.com> wrote in message

:
:

Phil Robyn

unread,
Mar 10, 2002, 3:00:25 AM3/10/02
to

If you type 'set /?' (without the apostrophes) at the CMD prompt, you will
see a lot of information, including the following:

May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

--

u n z i p m y a d d r e s s t o s e n d e - m a i l

Jerold Schulman

unread,
Mar 10, 2002, 9:08:44 AM3/10/02
to
On Thu, 7 Mar 2002 16:36:22 +0200, "Alex Blekhman" <tkfx....@yahoo.com>
wrote:

>Hi,

You might want to look at tip 4195 in the 'NT Reg Hacks' at
http://www.jsiinc.com


Jerold Schulman / MVP
JSI, Inc.
http://www.jsiinc.com

Clay Calvert

unread,
Mar 10, 2002, 10:21:41 AM3/10/02
to
On Sun, 10 Mar 2002 08:50:04 +0200, "Alex Blekhman"
<tkfx....@yahoo.com> wrote:

<snip>

>> for /L %%a in (1,1,120) do call:Three %%a
>> goto:eof
>> :Three
>> set Three=00%%a
> ^^^
> Did you mean %1 instead?

Yes, sorry. I was relying "The Force" to guide me. ; )

>> copy MyFile.txt \\Comp%Three:~-3%\C$\MyFile.txt
>>
>
>Thanks a lot. That was exactly what I need.
>
>And some OT question. Where did you get all this esoteric wisdom? The
>help provided with W2K is rather laconic about batch scripts.
>Definitely I didn't find there "%VarName:~-3%" trick.
>
>Thanks in advance
>Alex

The GUI Help is great for some things, like the Net commands, but
could be better in other areas. Like Alex and Phil mentioned, using
Set /? and For /? are the best built in ways to view variable
extraction techcniques.

I too was frustrated at the documentation available on variable
expansion, and put together the following which I keep as a text file
on my desktop.

***********

The following are examples of variable substitution and parsing in
Windows NT and Windows 2000.
A "#" character will indicate functionality only available in Windows
2000, and most likely XP.

More information can be found from running "Set /?" and "For /?".

A "one-sided" variable is a single character variable that has a
single percent sign preceding the character. Examples: %1 and %a


"Two-sided" variables can have more than one character and have a
percentage sign on both sides.
Examples: %temp% and %path%

The following string will be used for these examples:

set test=123456789abcdef0

Substitution [: and =] Two sided only

%PATH:str1=str2%
%PATH:str1=%
%PATH:*str1=str2%

%Test:ab=xy% 123456789xycdef0
%Test:ab=% 123456789cdef0
%Test:*ab=% cdef0
%Test:*ab=XY% XYcdef0


Parsing [%:~X% and %:~X,Y%] Two sided variables only

Extract only the first 5 characters
%test:~0,5%
12345

Skip 7 characters and then extract the next 5
%test:~7,5%
89abc

Skip 7 characters and then extract everything else
%test:~7%
89abcdef0

Extract only the last 7 characters #
%test:~-7%
abcdef0

Extract everything BUT the last 7 characters #
%test:~0,-7%
123456789

Extract between 7 from the front and 5 from the back #
%test:~7,-5%
89ab

Go back 7 from the end then extract 5 towards the end #
%test:~-7,5%
abcde

Extract between 7 from the end and 5 from the end #
%test:~-7,-5%
ab


[%~letter] One sided variables only

%~I removes any surrounding quotes (")
%~fI Fully qualified path name
%~dI Drive letter only
%~pI Path only
%~nI file Name only
%~xI eXtension only
%~sI Short names only
%~aI Attributes #
%~tI Time and date of file #
%~zI siZe of file #

[%~$string] Two-sided
%~$PATH:I searches the PATH and expands to the full name of the
first found

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the PATH and expands to the drive letter and
path of the first found
%~ftzaI - expands %I to a DIR like output line #

In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

**********

HTH,

Clay Calvert

unread,
Mar 10, 2002, 10:26:01 AM3/10/02
to
On Sun, 10 Mar 2002 02:57:52 -0500, "Alex K. Angelopoulos"
<alex_angelopoulos_at_hotmail_dot_com_remove__> wrote:

>Why don't you know? Clay was BORN in a command prompt!

LOL! Thanks Alex.

>Once you're "committed" to a command prompt solution for things, you start digging really hard, asking people, doing Internet searches, etc. Much of the information is actually there in documentation on your PC - just very hard to pull out; you can get some of it by trying /? on everything in sight.

Yep!

>But I would never have pulled that usage out of MY hat...

Sure you would have.... and it would have been syntactically accurate
and tested.

Cheers,

Alex Blekhman

unread,
Mar 10, 2002, 11:43:14 AM3/10/02
to
"Clay Calvert" <ccal...@Wanguru.com> wrote in message
news:b5tm8ug8kkvf6krj9...@4ax.com...
> [ ... ]

Thanks. This is really helpful. For some reason I always thought that
command prompt help is a subset of GUI help. I was surprised a lot
when 'set /?' dumped twenty times more lines than in GUI help.

Best wishes
Alex


Alex Blekhman

unread,
Mar 10, 2002, 11:46:09 AM3/10/02
to
"Jerold Schulman" <Je...@jsiinc.com> wrote in message
news:s8om8u0ho1bn6pi8e...@4ax.com...

Thanks, I'll take a look on that.


Al Dunbar

unread,
Mar 10, 2002, 12:02:59 PM3/10/02
to

"Alex Blekhman" <tkfx....@yahoo.com> wrote in message
news:#RCSaKFyBHA.1796@tkmsftngp02...

Hehehe. I think the command prompt development team realized that the only
people likely to be that interested in getting the most out of the command
prompt would be already working at the command prompt. Either that, or they
simply did not tell their bosses about the cmd extensions, thinking that,
rather than having that info added to the gui help, they would be told to
delete the functionality.

I had been using help and command/? for years before I even found out that
the basic help information is even present in the gui help ;-)


/Al

0 new messages