1

Why does this bat file forget %%a after :theFound? I am trying to understand how For /f works, but %%a is forgotten after the :theFound

Thanks for looking.

FOR /F %%a in (c:\temp\computers.txt) do (
echo %%a
set comPort=0
:comLoop
set /a comPort=%comPort%+1
reg query \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Pergamon\AKT\Dienst\XFS\PASION_CM24_COM%comPort% 
if errorlevel 0 goto theFound
if %comPort% LSS 10 goto comLoop
echo No CRU found >>c:\temp\output1.txt
:theFound
reg query \\%%a\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\WOSA/XFS_ROOT\SERVICE_PROVIDERS\PASION_CM24_COM%comPort%\Firmware>>c:\temp\output1.txt
)
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    don't use `goto` or labels inside a `for` loop. They break the loop (so of course all loop-variables are lost) – Stephan Apr 22 '16 at 20:06

1 Answers1

5

jumping inside a loop doesn't work, it breaks the loop. Instead you can call a subroutine (with %%a as parameter - in the subroutine it is referenced as %1 = "first parameter"). Inside the subroutine you can jump as much as you want:

FOR /F %%a in (c:\temp\computers.txt) do call :doit %%a
goto :eof

:doit
set comPort=0
:comLoop
set /a comPort=%comPort%+1
reg query \\%1\HKEY_LOCAL_MACHINE\SOFTWARE\Pergamon\AKT\Dienst\XFS\PASION_CM24_COM%comPort% 
if errorlevel 0 goto theFound
if %comPort% LSS 10 goto comLoop
echo No CRU found >>c:\temp\output1.txt
:theFound
reg query \\%1\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\WOSA/XFS_ROOT\SERVICE_PROVIDERS\PASION_CM24_COM%comPort%\Firmware>>c:\temp\output1.txt
goto :eof

(Bonus: you don't need delayed expansion)

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91