Variable not updating in loop

M

Matt

I'm not sure what is happening here, but my SP variable isn't returning
appropriately. I've extracted just the important bits from a larger script.
With the first loop iteration, the correct info is getting set, but after
that the SP variable is behind by one. The OSVer returns the appropriate
data. I've tried everything I can think of including resetting the variables
after the echo and echoing the variables inside the :GetRemoteOS sub but
neither work. The really odd thing is, if I turn on echoing, it shows the sp
variable being set to the right thing, it just doesn't echo it back that way
until the next iteration of the for loop. Can anyone help me figure out what
I'm doing wrong?

[1]@echo off
[2]setlocal ENABLEDELAYEDEXPANSION
[3]
[4]cd /d %~dp0
[5]
[6]if %1!==! goto :usage
[7]echo %1 | find /I "all" > nul
[8]if not errorlevel 1 (
[9] set arg='NET VIEW ^^^| FIND "\\"'
[10] goto :start)
[11]echo %1 | find "." > nul
[12]if not errorlevel 1 (set arg=%1) ELSE (set arg="\\%1")
[13]:start
[14]
[15]for /f %%a in (%arg%) do (
[16] set comp=%%a
[17] ping -n 1 !comp!|find "TTL=" > nul 2>&1
[18] Call :GetRemoteOS !comp! ver sp
[19] echo !comp! - !ver! - !sp!
[20] pause
[21])
[22]goto :Eof
[23]
[24]:GetRemoteOS CompName OSVer SP
[25]:: 4.0 = NT4 5.1=XP 5.2=2K3 ::
[26]set Cname=%~1
[27]if %Cname:~0,2%==\\ set Cname=%Cname:\\=%
[28]set key=REG QUERY "\\%Cname%\HKLM\SOFTWARE\Microsoft\Windows
NT\CurrentVersion"
[29]ping -n 1 %cname% |find "TTL=" > nul 2>&1
[30]if not errorlevel = 1 (
[31] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r "ProductName.*REG"
') do (set pn=%%c %%d)
[32] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r "CSDVersion.*REG" ')
do (set %3=%pn% %%c %%d)
[33] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r
"CurrentVersion.*REG" ') do (set %2=%%c)
[34])
[35]endlocal & (goto :EOF)
[36]
[37]:usage
[38]echo.
[39]echo Usage^: %~n0 ^[ALL^] uses all computers listed with 'net view'.
[40]echo ^[MachineName^] uses specified machine (no \\ necessary).
[41]echo ^[Path\File^] will get computer names from a file on disk.
[42]echo.&echo Examples:&echo.
[43]echo %~n0 ALL
[44]echo %~n0 COMPUTER1
[45]echo %~n0 C:\ListofComputers.txt
[46]goto :eof

TIA

Matt
 
B

billious

Matt said:
I'm not sure what is happening here, but my SP variable isn't returning
appropriately. I've extracted just the important bits from a larger
script.
With the first loop iteration, the correct info is getting set, but after
that the SP variable is behind by one. The OSVer returns the appropriate
data. I've tried everything I can think of including resetting the
variables
after the echo and echoing the variables inside the :GetRemoteOS sub but
neither work. The really odd thing is, if I turn on echoing, it shows the
sp
variable being set to the right thing, it just doesn't echo it back that
way
until the next iteration of the for loop. Can anyone help me figure out
what
I'm doing wrong?

[1]@echo off
[2]setlocal ENABLEDELAYEDEXPANSION
[3]
[4]cd /d %~dp0
[5]
[6]if %1!==! goto :usage
[7]echo %1 | find /I "all" > nul
[8]if not errorlevel 1 (
[9] set arg='NET VIEW ^^^| FIND "\\"'
[10] goto :start)
[11]echo %1 | find "." > nul
[12]if not errorlevel 1 (set arg=%1) ELSE (set arg="\\%1")
[13]:start
[14]
[15]for /f %%a in (%arg%) do (
[16] set comp=%%a
[17] ping -n 1 !comp!|find "TTL=" > nul 2>&1
[18] Call :GetRemoteOS !comp! ver sp
[19] echo !comp! - !ver! - !sp!
[20] pause
[21])
[22]goto :Eof
[23]
[24]:GetRemoteOS CompName OSVer SP
[25]:: 4.0 = NT4 5.1=XP 5.2=2K3 ::
[26]set Cname=%~1
[27]if %Cname:~0,2%==\\ set Cname=%Cname:\\=%
[28]set key=REG QUERY "\\%Cname%\HKLM\SOFTWARE\Microsoft\Windows
NT\CurrentVersion"
[29]ping -n 1 %cname% |find "TTL=" > nul 2>&1
[30]if not errorlevel = 1 (
[31] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r "ProductName.*REG"
') do (set pn=%%c %%d)
[32] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r "CSDVersion.*REG"
')
do (set %3=%pn% %%c %%d)
[33] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r
"CurrentVersion.*REG" ') do (set %2=%%c)
[34])
[35]endlocal & (goto :EOF)
[36]
[37]:usage
[38]echo.
[39]echo Usage^: %~n0 ^[ALL^] uses all computers listed with 'net view'.
[40]echo ^[MachineName^] uses specified machine (no \\ necessary).
[41]echo ^[Path\File^] will get computer names from a file on disk.
[42]echo.&echo Examples:&echo.
[43]echo %~n0 ALL
[44]echo %~n0 COMPUTER1
[45]echo %~n0 C:\ListofComputers.txt
[46]goto :eof

TIA

Matt

Line [32]

pn is evaluated on parsing [30] and will be NULL on the first entry into
[24]
change %pn% in [32] to !pn!

Line [35]

Endlocal is executed for each invocation of GetRemoteOS, but setlocal is
only executed once in [2]

Change [35] to

goto :eof
 
B

billious

billious said:
Matt said:
I'm not sure what is happening here, but my SP variable isn't returning
appropriately. I've extracted just the important bits from a larger
script.
With the first loop iteration, the correct info is getting set, but after
that the SP variable is behind by one. The OSVer returns the appropriate
data. I've tried everything I can think of including resetting the
variables
after the echo and echoing the variables inside the :GetRemoteOS sub but
neither work. The really odd thing is, if I turn on echoing, it shows the
sp
variable being set to the right thing, it just doesn't echo it back that
way
until the next iteration of the for loop. Can anyone help me figure out
what
I'm doing wrong?

[1]@echo off
[2]setlocal ENABLEDELAYEDEXPANSION
[3]
[4]cd /d %~dp0
[5]
[6]if %1!==! goto :usage
[7]echo %1 | find /I "all" > nul
[8]if not errorlevel 1 (
[9] set arg='NET VIEW ^^^| FIND "\\"'
[10] goto :start)
[11]echo %1 | find "." > nul
[12]if not errorlevel 1 (set arg=%1) ELSE (set arg="\\%1")
[13]:start
[14]
[15]for /f %%a in (%arg%) do (
[16] set comp=%%a
[17] ping -n 1 !comp!|find "TTL=" > nul 2>&1
[18] Call :GetRemoteOS !comp! ver sp
[19] echo !comp! - !ver! - !sp!
[20] pause
[21])
[22]goto :Eof
[23]
[24]:GetRemoteOS CompName OSVer SP
[25]:: 4.0 = NT4 5.1=XP 5.2=2K3 ::
[26]set Cname=%~1
[27]if %Cname:~0,2%==\\ set Cname=%Cname:\\=%
[28]set key=REG QUERY "\\%Cname%\HKLM\SOFTWARE\Microsoft\Windows
NT\CurrentVersion"
[29]ping -n 1 %cname% |find "TTL=" > nul 2>&1
[30]if not errorlevel = 1 (
[31] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r
"ProductName.*REG"
') do (set pn=%%c %%d)
[32] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r "CSDVersion.*REG"
')
do (set %3=%pn% %%c %%d)
[33] for /f "Tokens=1,2,3*" %%a in (' %key%^|findstr /r
"CurrentVersion.*REG" ') do (set %2=%%c)
[34])
[35]endlocal & (goto :EOF)
[36]
[37]:usage
[38]echo.
[39]echo Usage^: %~n0 ^[ALL^] uses all computers listed with 'net view'.
[40]echo ^[MachineName^] uses specified machine (no \\ necessary).
[41]echo ^[Path\File^] will get computer names from a file on disk.
[42]echo.&echo Examples:&echo.
[43]echo %~n0 ALL
[44]echo %~n0 COMPUTER1
[45]echo %~n0 C:\ListofComputers.txt
[46]goto :eof

TIA

Matt

Line [32]

pn is evaluated on parsing [30] and will be NULL on the first entry into
[24]
change %pn% in [32] to !pn!

Line [35]

Endlocal is executed for each invocation of GetRemoteOS, but setlocal is
only executed once in [2]

Change [35] to

goto :eof
Note also that if line [30] fails to execute the TRUE condition, the
parameters VER and SP will not change, and you will repeat the results of
the previous iteration for the current one. You need to explicitly set VER
and SP (or %2, %3) under these circumstances.
 

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