simple Batch File questions

W

WickedShark

I have a batch file that I edit every time I need to use it. It is a
very simple file that needs to have a little more intelligence with
user interaction.


Here is what the batch file does now.

@echo off
start c:\riom.bat *** a
#start c:\riom.bat *** b
#start c:\riom.bat *** c
#start c:\riom.bat *** d
#start c:\riom.bat *** e
#start c:\riom.bat *** f
#start c:\riom.bat *** g

Now when I run this I have to uncomment or comment out the number of
the batch files I want to run and then I need to enter the variable
"***" which is actually the last octet of an IP address.

What I would like to batch file to do is ask me how many sessions of
the riom.bat I want to run and then let me put the IP variable in
place.

Is this possible?
 
F

foxidrive

I have a batch file that I edit every time I need to use it. It is a
very simple file that needs to have a little more intelligence with
user interaction.


Here is what the batch file does now.

@echo off
start c:\riom.bat *** a
#start c:\riom.bat *** b
#start c:\riom.bat *** c
#start c:\riom.bat *** d
#start c:\riom.bat *** e
#start c:\riom.bat *** f
#start c:\riom.bat *** g

Now when I run this I have to uncomment or comment out the number of
the batch files I want to run and then I need to enter the variable
"***" which is actually the last octet of an IP address.

What I would like to batch file to do is ask me how many sessions of
the riom.bat I want to run and then let me put the IP variable in
place.

You could use a batch file that starts with a list of IP octets and let it
process them. Can you show us riom.bat? There might be a neater solution.


Alternatively you can use a flat text file containing this sort of thing:

123 a
124 b
125 c
126 d

and use this:

@echo off
for /f "tokens=1,2" %%a in (file.txt) do start "" c:\riom.bat %%a %%b
 
W

WickedShark

I have a batch file that I edit every time I need to use it. It is a
very simple file that needs to have a little more intelligence with
user interaction.
Here is what the batch file does now.
@echo off
start c:\riom.bat *** a
#start c:\riom.bat *** b
#start c:\riom.bat *** c
#start c:\riom.bat *** d
#start c:\riom.bat *** e
#start c:\riom.bat *** f
#start c:\riom.bat *** g
Now when I run this I have to uncomment or comment out the number of
the batch files I want to run and then I need to enter the variable
"***" which is actually the last octet of an IP address.
What I would like to batch file to do is ask me how many sessions of
the riom.bat I want to run and then let me put the IP variable in
place.
Is this possible?

This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]for %%i in (sessions octet) do (set %%i=)
[3]set /p octet=Enter octet ?
[4]if not defined octet goto end
[5]set /p sessions=Enter number of sessions ?
[6]if not defined sessions goto end
[7]set sessparms=abcdefghijklmnopqrstuvwxyz
[8]:loop
[9]if %sessions%==0 goto end
[10]ECHO start %octet% %sessparms:~0,1%
[11]set sessparms=%sessparms:~1%
[12]set /a sessions-=1
[13]goto loop
[14]
[15]:end
[16]for %%i in (sessparms sessions octet) do (set %%i=)
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO  keyword needs to be removed to activate the START It is thereas a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

Note that [2] and [16] simply keep the environment clean. In each, the
parentheses around the SET are not strictly required - this guards against
stray trailing-spaces.

The input is unchecked.

See

SET /?

from the prompt for docco

Here's another approach:

----- batch begins -------
[1]@echo off
[2]setlocal enableextensions enabledelayedexpansion
[3]for %%i in (sessions octet) do (set %%i=)
[4]set /p octet=Enter octet ?
[5]if not defined octet goto:eof
[6]set /p sessions=Enter number of sessions ?
[7]if not defined sessions goto :eof
[8]set sesscount=0
[9]for %%i in (first second third fourth) do set /a sesscount+=1&if
!sesscount! leq %sessions% ECHO start %octet% %%i
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO  keyword needs to be removed to activate the START It is thereas a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

See

SETLOCAL /?

for docco.

Note that the SETLOCAL in [2] obviates the need for environment clean-up
(reaching EOF is an implicit ENDLOCAL)

Obviously, the list first..fourth in [9] should be expanded - and may be a b
c ... if that's your preference.

Note also that in both these examples, deliberately appending spaces to the
SET /p prompts will make the input nicer.
Unfortunately, some editors like dumping trailing spaces (and NOTEPAD, for
example tries to reformat - Eschew NOTEPAD, use EDIT IMHO.) You can get
around this by using something like

set space= x
set space=%space:~0,1%
...
set /p var=Prompt string%space%

...see alt.msdos.batch.nt for examples galore.

Thanks for your replies. I will look into these options and see what
works best. I am actually using 2k3 so XP solutions should work fine.
 
W

WickedShark

I have a batch file that I edit every time I need to use it. It is a
very simple file that needs to have a little more intelligence with
user interaction.
Here is what the batch file does now.
@echo off
start c:\riom.bat *** a
#start c:\riom.bat *** b
#start c:\riom.bat *** c
#start c:\riom.bat *** d
#start c:\riom.bat *** e
#start c:\riom.bat *** f
#start c:\riom.bat *** g
Now when I run this I have to uncomment or comment out the number of
the batch files I want to run and then I need to enter the variable
"***" which is actually the last octet of an IP address.
What I would like to batch file to do is ask me how many sessions of
the riom.bat I want to run and then let me put the IP variable in
place.
Is this possible?

This solution developed using XP
It may work for NT4/2K

----- batch begins -------
[1]@echo off
[2]for %%i in (sessions octet) do (set %%i=)
[3]set /p octet=Enter octet ?
[4]if not defined octet goto end
[5]set /p sessions=Enter number of sessions ?
[6]if not defined sessions goto end
[7]set sessparms=abcdefghijklmnopqrstuvwxyz
[8]:loop
[9]if %sessions%==0 goto end
[10]ECHO start %octet% %sessparms:~0,1%
[11]set sessparms=%sessparms:~1%
[12]set /a sessions-=1
[13]goto loop
[14]
[15]:end
[16]for %%i in (sessparms sessions octet) do (set %%i=)
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO  keyword needs to be removed to activate the START It is thereas a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

Note that [2] and [16] simply keep the environment clean. In each, the
parentheses around the SET are not strictly required - this guards against
stray trailing-spaces.

The input is unchecked.

See

SET /?

from the prompt for docco

Here's another approach:

----- batch begins -------
[1]@echo off
[2]setlocal enableextensions enabledelayedexpansion
[3]for %%i in (sessions octet) do (set %%i=)
[4]set /p octet=Enter octet ?
[5]if not defined octet goto:eof
[6]set /p sessions=Enter number of sessions ?
[7]if not defined sessions goto :eof
[8]set sesscount=0
[9]for %%i in (first second third fourth) do set /a sesscount+=1&if
!sesscount! leq %sessions% ECHO start %octet% %%i
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The ECHO  keyword needs to be removed to activate the START It is thereas a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

%varname% will be evaluated as the value of VARNAME at the time that the
line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

See

SETLOCAL /?

for docco.

Note that the SETLOCAL in [2] obviates the need for environment clean-up
(reaching EOF is an implicit ENDLOCAL)

Obviously, the list first..fourth in [9] should be expanded - and may be a b
c ... if that's your preference.

Note also that in both these examples, deliberately appending spaces to the
SET /p prompts will make the input nicer.
Unfortunately, some editors like dumping trailing spaces (and NOTEPAD, for
example tries to reformat - Eschew NOTEPAD, use EDIT IMHO.) You can get
around this by using something like

set space= x
set space=%space:~0,1%
...
set /p var=Prompt string%space%

...see alt.msdos.batch.nt for examples galore.

Both of these seem to work great and very close to what I am looking
for. I think I missed one important piece of information though.

The IP octets will all be different with each run of the script or
could be different in each run so I really need the script to ask the
number of sessions which it does and then I need to be able to put the
octet for each session. Does this make sense?
 
W

WickedShark

Clear as mud.

You now appear to have a list of octets that you wish to process, but you
don't say what the second parameter to RIOM.BAT needs to be. Is it fixed,
depends on the octet, always the same for any given octet, user-input or
what?

Foxi's approach, from a data-file is probably easiest although the line is
possibly better written

for /f "delims=" %%i in (file.txt) do start RION.BAT %%i

OR, given that you may want to INPUT data, use the general input sequence

(set var=)
set /p var=promptstring

* This "sets the variable to a zero-length string" (actually, deletes it
from the environment) before the SET/P. SET/P will NOT change var if the
user response is ENTER. ie. if VAR contains SOMEVALUE then

set /p var=promptstring

with a response of ENTER will leave var set to SOMEVALUE

* You can then check whether any entry was made with

if defined var goto ...

* or if NOT defined ....

Using this as a building-block, you could then use

if note defined var echo Nothing entered&goto :eof
for %%i in (%var%) do start RIOM.BAT %%i something

This allows you to enter a list as VAR and invoke RIOM for each entry in the
list and second parameter fixed=something.

(* obviously here, you could also prompt for SOMETHING beforehand and use
%something% as a parmeter to riom)

so an entry of

123 456 654 321

to the prompt would invoke RIOM 4 times

123 something
456 something
654 something
321 something

But we don't know whether you want to change SOMETHING each time...

If THAT's what you want to do, then the for /f "tokens=1,2" %%i in
(octetdata.txt)... solution is probably easiest, but you could, if you wish,
prompt for each parameter.

OR you may want to prompt for octet, parameter in turn, n times

OR you want to may enter the octet and parameter on the same line:

:loop
(set var=)
set /p var=octet space parameter (enter to stop)
if not defined var goto :eof
call RIOM.BAT %var%
goto loop

Of course, you might also want parameter of SOMETHING1 for octet1, octet2
and octet3 but SOMETHING2 for octet4 and octet5

The possibilities are extensive. More info needed - or possibly a different
approach to entering the data is more appropriate.

Some examples might be good...

Ok I think if I explain with more detail exactly what I need to do it
will help. Thanks for the help so far though it has been great. Also I
am new to this so I am looking for a good reference website or book to
get. Advice here would be great to.

Here goes. I am a QA engineer and what I am setting up is a program
called IOMeter. The RIOM.bat is a quick batch file I wrote that start
was is called a Dynamo session which generates network traffic. See
below.

RIOM.bat is here
C:\IOMETER\Dynamo /i 192.168.200.%1 /m 192.168.200.64 /n 12%2

Dynamo is the program or session I am starting. This is an .exe file
that needs 3 parameters setup. /i /m /n/
/i is the IP address of host or non-dynamo server. (the other end of
the connection that is sending or receiving the network traffic. (I
have 40 or so different host servers that have multiple NICs in them
so each server can have up to 4 or more IP addresses that all need
there own Dynamo session. (Reason for different octet IP's)
/m is the computer name or IP address that Dynamo is running on. (I
have 15 of these clients that all run multiple Dynamo sessions (could
be 10 of them which is why I am asking how many)
/n is just a simple name which in my case the #12 above just happens
to be the client number I am using and the %2 is the a,b,c,d,e,f piece
of the name. (this shows up on the host side so I know which client my
host is seeing and which Dynamo session it connects to.

So going back to my first question.

What I need and I am open to a completely new way of doing this.

I want to be able to run the batch file on each of my traffic
servers.
1 input the number of dynamo sessions I want to run
2 input using a list might be best so I can or others can just edit a
list of IP's (last octet only) without editing the script.


Does this help and sorry for the long post. Not sure else how to
explain what I am trying to accomplish. What I have works but it is
clunky and not very intelligent and is prone to errors by others that
do not understand how Dynamo needs to be setup. Asking simple
questions and populating a list anyone can do well most anyways.
 
F

foxidrive

Here goes. I am a QA engineer and what I am setting up is a program
called IOMeter. The RIOM.bat is a quick batch file I wrote that start
was is called a Dynamo session which generates network traffic. See
below.

RIOM.bat is here
C:\IOMETER\Dynamo /i 192.168.200.%1 /m 192.168.200.64 /n 12%2

Dynamo is the program or session I am starting. This is an .exe file
that needs 3 parameters setup. /i /m /n/
/i is the IP address of host or non-dynamo server. (the other end of
the connection that is sending or receiving the network traffic. (I
have 40 or so different host servers that have multiple NICs in them
so each server can have up to 4 or more IP addresses that all need
there own Dynamo session. (Reason for different octet IP's)
/m is the computer name or IP address that Dynamo is running on. (I
have 15 of these clients that all run multiple Dynamo sessions (could
be 10 of them which is why I am asking how many)
/n is just a simple name which in my case the #12 above just happens
to be the client number I am using and the %2 is the a,b,c,d,e,f piece
of the name. (this shows up on the host side so I know which client my
host is seeing and which Dynamo session it connects to.

So going back to my first question.

What I need and I am open to a completely new way of doing this.

I want to be able to run the batch file on each of my traffic
servers.
1 input the number of dynamo sessions I want to run
2 input using a list might be best so I can or others can just edit a
list of IP's (last octet only) without editing the script.
Does this help and sorry for the long post.

Can I ask how the set of IP's are arranged? Are the octets contiguous?

Would you use the range from 101 to 109 for example, or would they be
picked and plucked from all over the entire range?
 
W

WickedShark

Can I ask how the set of IP's are arranged?  Are the octets contiguous?

Would you use the range from 101 to 109 for example, or would they be
picked and plucked from all over the entire range?

Good question. At this point all the IP's sit on a 200.*** subnet. So
for example each of the host servers (Non Dynamo) will have 4 IP's per
server assinged to them but they are not all used in a test.

So each server may look like this.
Server 1 192.168.200.100-103
Server 2 192.168.200.104-107
Server 3 192.168.200.108-111

I may setup a test that consists of the following setup
Server 1 100, 101
Server 2 104, 105
Server 1 109, 108

Does this make sense?
 
F

foxidrive

Good question. At this point all the IP's sit on a 200.*** subnet. So
for example each of the host servers (Non Dynamo) will have 4 IP's per
server assinged to them but they are not all used in a test.

So each server may look like this.
Server 1 192.168.200.100-103
Server 2 192.168.200.104-107
Server 3 192.168.200.108-111

I may setup a test that consists of the following setup
Server 1 100, 101
Server 2 104, 105
Server 1 109, 108

Does this make sense?

Yep. It seems to be a particularly dynamic setup so perhaps this would suit you:
It would use the IP octet as the session name, and you'd just enter as many sessions as
you need at a time.


@echo off
:loop
set /p "ip=enter last octet of dynamo target (0 to quit): "
if "%ip%"=="0" goto :EOF
start "" /min C:\IOMETER\Dynamo /i 192.168.200.%ip% /m 192.168.200.64 /n session-%ip%
goto :loop
 
W

WickedShark

Yep.  It seems to be a particularly dynamic setup so perhaps this wouldsuit you:
It would use the IP octet as the session name, and you'd just enter as many sessions as
you need at a time.

@echo off
:loop
set /p "ip=enter last octet of dynamo target (0 to quit): "
if "%ip%"=="0" goto :EOF
start "" /min C:\IOMETER\Dynamo /i 192.168.200.%ip% /m 192.168.200.64 /n session-%ip%
goto :loop

This would work but I actually a combination of what you put first and
what billious put.

@echo off
for /f "tokens=1,2" %%a in (file.txt) do start "" c:\riom.bat %%a %%b

[1]@echo off
[2]for %%i in (sessions octet) do (set %%i=)
[3]set /p octet=Enter octet ?
[4]if not defined octet goto end
[5]set /p sessions=Enter number of sessions ?
[6]if not defined sessions goto end
[7]set sessparms=abcdefghijklmnopqrstuvwxyz
[8]:loop
[9]if %sessions%==0 goto end
[10]ECHO start %octet% %sessparms:~0,1%
[11]set sessparms=%sessparms:~1%
[12]set /a sessions-=1
[13]goto loop
[14]
[15]:end
[16]for %%i in (sessparms sessions octet) do (set %%i=)

I like the idea of processing the IP octects with an iplist.txt but I
also like this piece set sessparms=abcdefghijklmnopqrstuvwxyz.

This way it processes each IP octect and I get the a b c d e session
parameter. If this can be done than I think I have what I need for
now.

Can either one recommend a good book for batch file scripting? As you
can see mine is very limited.

Thanks
 
F

foxidrive

I like the idea of processing the IP octects with an iplist.txt but I
also like this piece set sessparms=abcdefghijklmnopqrstuvwxyz.

This way it processes each IP octect and I get the a b c d e session
parameter. If this can be done than I think I have what I need for
now.

This borrows billious' technique:

@echo off
setlocal EnableDelayedExpansion
set sessparms=abcdefghijklmnopqrstuvwxyz
set session=
for /f %%a in (file.txt) do (
set session=!sessparms:~0,1!
set sessparms=!sessparms:~1!
start "" c:\riom.bat %%a !session!
)
Can either one recommend a good book for batch file scripting? As you
can see mine is very limited.

I don't know of a good book - I'd recommend modifying scripts that already exist, to fine
tune them and enhance them, and posting here or in alt.msdos.batch.nt (preferred) when
problems crop up. Learning from hands on experience is a good technique for batch so you
can integrate the "gotcha's" and undocumented tricks as they appear.
 
W

WickedShark

This borrows billious' technique:

@echo off
setlocal EnableDelayedExpansion
set sessparms=abcdefghijklmnopqrstuvwxyz
set session=
for /f %%a in (file.txt) do (
set session=!sessparms:~0,1!
set sessparms=!sessparms:~1!
start "" c:\riom.bat %%a !session!
)


I don't know of a good book - I'd recommend modifying scripts that already exist, to fine
tune them and enhance them, and posting here or in alt.msdos.batch.nt (preferred) when
problems crop up.  Learning from hands on experience is a good technique for batch so you
can integrate the "gotcha's" and undocumented tricks as they appear.

Thank you for your help that is exactly what I was going for this time
around.
 
W

WickedShark

This borrows billious' technique:

@echo off
setlocal EnableDelayedExpansion
set sessparms=abcdefghijklmnopqrstuvwxyz
set session=
for /f %%a in (file.txt) do (
set session=!sessparms:~0,1!
set sessparms=!sessparms:~1!
start "" c:\riom.bat %%a !session!
)


I don't know of a good book - I'd recommend modifying scripts that already exist, to fine
tune them and enhance them, and posting here or in alt.msdos.batch.nt (preferred) when
problems crop up.  Learning from hands on experience is a good technique for batch so you
can integrate the "gotcha's" and undocumented tricks as they appear.

Ok so the script is working great. Now I see one issue though. Many
times I have to start and stop the Dynamo sessions. Is there a way to
put a check in to see if an IP from the list is already running? This
way it will not start that IP again. I can edit the list each time I
want to stop and start a failed or stopped test but it would be easier
to just run the script again and have it check to see what IP's are
running and then not start them or just start the ones that are not
running.
 
F

foxidrive

Ok so the script is working great. Now I see one issue though. Many
times I have to start and stop the Dynamo sessions. Is there a way to
put a check in to see if an IP from the list is already running? This
way it will not start that IP again. I can edit the list each time I
want to stop and start a failed or stopped test but it would be easier
to just run the script again and have it check to see what IP's are
running and then not start them or just start the ones that are not
running.

This adds the session name to the window title and will skip the IP if the
process with the windowtitle is still running.

You'd just need to close the failed/stopped sessions.



tasklist comes with XP pro and up I think.

@echo off
setlocal EnableDelayedExpansion
set sessparms=abcdefghijklmnopqrstuvwxyz
set session=
for /f %%a in (file.txt) do (
set session=!sessparms:~0,1!
set sessparms=!sessparms:~1!
tasklist /FI "WINDOWTITLE eq dynamosession!session!" 2>nul|find "Session">nul
if errorlevel 1 start "dynamosession!session!" c:\riom.bat %%a !session!
)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top