CMD bat为什么%% a在第一次循环后会丢失它的值?

1 cmd batch-file

为什么这个bat文件忘记了%% a after:theFound?我试图理解For/f是如何工作的,但是在%thefound之后忘记了%% a

谢谢你的期待.

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
)
Run Code Online (Sandbox Code Playgroud)

Ste*_*han 5

在循环内跳转不起作用,它打破了循环.相反,你可以调用一个子程序(带%%a参数 - 在子程序中它被引用为%1="第一个参数").在子程序中,您可以根据需要跳转:

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
Run Code Online (Sandbox Code Playgroud)

(额外奖励:您不需要延迟扩展)