Pass 2 variables into dos FOR command

  • Thread starter Thread starter Kathy Houtami
  • Start date Start date
K

Kathy Houtami

Hi

I'm just wondering if there is a way to pass 2 variables into the dos
For command. I'm trying to script updating a scheduled job across
multiple servers, so each will have separate start time.

for /F %%i in (NDC.txt Times.txt) do CALL SchTask.bat %%i %%j
"%AdminPass%"


Schtasks /change /s \\%1 /U %1\administrator /P %3 /TN IISLogArchiver /
RU %1\administrator /RP %3 /ST %2 /ENABLE


The above is what I tried to do, but when I ran the script, the %2
didnt pick up the variables stored in second txt file (Times.txt).

Please help


Cheers
Kathy
 
Kathy Houtami said:
Hi

I'm just wondering if there is a way to pass 2 variables into the dos
For command. I'm trying to script updating a scheduled job across
multiple servers, so each will have separate start time.

for /F %%i in (NDC.txt Times.txt) do CALL SchTask.bat %%i %%j
"%AdminPass%"


Schtasks /change /s \\%1 /U %1\administrator /P %3 /TN IISLogArchiver /
RU %1\administrator /RP %3 /ST %2 /ENABLE


The above is what I tried to do, but when I ran the script, the %2
didnt pick up the variables stored in second txt file (Times.txt).

Please help


Cheers
Kathy

Not really sure what the format of your files is - it would seem more
rational to simply have a single file containing lines of

server1 time1
server2 time2

etc.

Is it that your files contain servers and times such that you want
corresponding lines to be matched, that is, the data on line 1 of NDC.txt to
be matched with the data on line 1 of the times.txt file? This structure
would seem very unstable...but here's a way to process it...

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

----- batch begins -------
[1]@echo off
[2]for /f "tokens=1*delims=][" %%i in ( ' find /v /n "" ndc.txt ' ) do (
[3] for /f "tokens=1*delims=][" %%a in ( ' find /v /n "" times.txt ' ) do (
[4] if %%i==%%a ECHO call schtask.bat %%j %%b "%adminpass%"
[5] )
[6])
------ 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 CALL It is there as a
safety measure to show what the process WOULD do until
you have verified that it will do what you require

The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.

Unfortunately, matching EACH line in times.txt to those in NDC.txt would
seem to violate your aim "so each will have separate start time."
 
Kathy Houtami said:
Hi

I'm just wondering if there is a way to pass 2 variables into the dos
For command. I'm trying to script updating a scheduled job across
multiple servers, so each will have separate start time.

for /F %%i in (NDC.txt Times.txt) do CALL SchTask.bat %%i %%j
"%AdminPass%"


Schtasks /change /s \\%1 /U %1\administrator /P %3 /TN IISLogArchiver /
RU %1\administrator /RP %3 /ST %2 /ENABLE


The above is what I tried to do, but when I ran the script, the %2
didnt pick up the variables stored in second txt file (Times.txt).

In order for the FOR command to emit more than one variable, you have to
specify which tokens you want from the input, with the "tokens=" option.
Here is how I would do it:

for /f "tokens=1,2" %%i in (NdcTimes.txt) do call:SchTask %%i %%2
"%AdminPass%"
goto:eof

:SchTask
Schtasks /change /s \\%1 /U %1\administrator /P %3 /TN
IISLogArchiver /RU %1\administrator /RP %3 /ST %2 /ENABLE

goto:eof

The data file NdcTimes.txt would contain the two values as the first and
second words, i.e.:

server1 18:01
server2 19:11

You seem to be assuming that you need two input files, one for each
parameter, but that is not how FOR works. When it process a list of files,
it finishes with one before it starts on the next. And it gets all of the
tokens or parameters from each line it processes.

/Al
 
Al Dunbar said:
In order for the FOR command to emit more than one variable, you have to
specify which tokens you want from the input, with the "tokens=" option.
Here is how I would do it:

for /f "tokens=1,2" %%i in (NdcTimes.txt) do call:SchTask %%i %%2
"%AdminPass%"
goto:eof

:SchTask
Schtasks /change /s \\%1 /U %1\administrator /P %3 /TN
IISLogArchiver /RU %1\administrator /RP %3 /ST %2 /ENABLE

goto:eof

The data file NdcTimes.txt would contain the two values as the first and
second words, i.e.:

server1 18:01
server2 19:11

You seem to be assuming that you need two input files, one for each
parameter, but that is not how FOR works. When it process a list of files,
it finishes with one before it starts on the next. And it gets all of the
tokens or parameters from each line it processes.

/Al

Forgot to mention that, since the same password is used for each server, it
need not be passed as a parameter, but as a global variable (however much I
dislike using global variables), i.e.:

for /f "tokens=1,2" %%i in (NdcTimes.txt) do call:SchTask %%i %%2
goto:eof

:SchTask
Schtasks /change /s \\%1 /U %1\administrator /P %AdminPass% /TN
IISLogArchiver /RU %1\administrator /RP %AdminPass% /ST %2 /ENABLE
goto:eof

Also forgot to mention that I used an "internal" subroutine called
":SchTask" instead of the external batch file, SchTask.bat.

/Al
 
Thank you

It works like a charm!!!!

Forgot to mention that, since the same password is used for each server, it
need not be passed as a parameter, but as a global variable (however much I
dislike using global variables), i.e.:

for /f "tokens=1,2" %%i in (NdcTimes.txt) do call:SchTask %%i %%2
goto:eof

:SchTask
Schtasks /change /s \\%1 /U %1\administrator /P %AdminPass% /TN
IISLogArchiver /RU %1\administrator /RP %AdminPass% /ST %2 /ENABLE
goto:eof

Also forgot to mention that I used an "internal" subroutine called
":SchTask" instead of the external batch file, SchTask.bat.

/Al
 
Back
Top