Nested Delayed Expansion Variables in Batch

August 2, 2011 at 10:30:16
Specs: Windows 7, Xeon x5550 @ 2.67 GHz/12 GB
Hello,

I'm writing a batch file to pull in information from two files - one that contains computer names and one that contains ip addresses. I want to store each of these as variables, and then execute a loop that runs qwinsta over each machine and echos the machine name and ip address for each.

(I know, only one of these fields is necessary for the qwinsta command, but the bosses want both in the log file).

I am, at the moment, using the following:

SET LOG=..\output\available_pcs.log

DEL /F /Q ..\output\

REM LOG START
ECHO available_pcs.bat started	>%LOG%
ECHO %DATE%		>>%LOG%
ECHO %TIME%		>>%LOG%

REM PRECLEAN.DO
%STATAEXE% /e do preclean.do
COPY %LOG%+preclean.log %LOG%
DEL preclean.log

SetLocal EnableDelayedExpansion

Set n= >>%LOG%
Set _InputFile=shared_names.txt
For /F "tokens=*" %%A IN (%_InputFile%) DO (
	Set /a n+=1
	Set _var!n!=%%A
	Set _nvarend=!n!
)

Set m=!n! >>%LOG%
Set _InputFile=shared_ips.txt
For /F "tokens=*" %%M IN (%_InputFile%) DO (
	Set /a m+=1
	Set _var!m!=%%M
)

Set _ >>%LOG%

Set m=!n! >>%LOG%
Set /a m+=1 >>%LOG%
For /l %%X IN (1,1,%_nvarend%) DO (
	ECHO Checking !_var%%X! at !_var!m!! for availability >>%LOG%
	qwinsta /server:!_var%%X! >>%LOG% 2>&1
	Set /a m+=1 >>%LOG%
)

EndLocal

REM COMPLETE

ECHO available_pcs.bat completed	>>%LOG%
ECHO %DATE%		>>%LOG%
ECHO %TIME%		>>%LOG%

PAUSE

The issue comes with the _var!m! in the final loop. While !_var%%X! updates correctly with each iteration of the loop, I can't figure out how to nest the variables for delayed expansion. !_var!m!! simply gets displayed as "m", while %_var!m!% displays nothing, and !_var%m%! does not, of course, update after the first iteration.

Is there a way to nest delayed expansion variables so that this will properly display the IP address in a log file?

Thanks.


See More: Nested Delayed Expansion Variables in Batch

Report •

#1
August 2, 2011 at 11:01:32
Nevermind!

Restructured the code to the following:

REM ****************************************************
REM * check_available_pcs.bat: double-click to run
REM ****************************************************

SET LOG=..\output\available_pcs.log

DEL /F /Q ..\output\
RMDIR ..\temp\
MKDIR ..\temp\

REM LOG START
ECHO available_pcs.bat started	>%LOG%
ECHO %DATE%		>>%LOG%
ECHO %TIME%		>>%LOG%

REM PRECLEAN.DO
%STATAEXE% /e do preclean.do
DEL preclean.log

REM Create variable list for Computer Names

SetLocal EnableDelayedExpansion

Set n=
Set _InputFile=..\temp\shared_names.txt
For /F "tokens=*" %%A IN (%_InputFile%) DO (
	Set /a n+=1
	Set _name!n!=%%A
	Set _nameend=!n!
)

REM Create variable list for IP Address

Set n=
Set _InputFile=..\temp\shared_ips.txt
For /F "tokens=*" %%M IN (%_InputFile%) DO (
	Set /a n+=1
	Set _ip!n!=%%M
)

REM Run QWINSTA on all machines

For /l %%X IN (1,1,%_nameend%) DO (
	ECHO Checking !_name%%X! at !_ip%%X! for availability >>%LOG%
	qwinsta /server:!_ip%%X! >>%LOG% 2>&1
)

EndLocal

REM COMPLETE

ECHO available_pcs.bat completed	>>%LOG%
ECHO %DATE%		>>%LOG%
ECHO %TIME%		>>%LOG%

PAUSE

Works like a charm now.


Report •
Related Solutions


Ask Question