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.
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.