2016-04-22 85 views
1

爲什麼這個bat文件忘了%% a之後:theFound?我試圖瞭解For/f是如何工作的,但%% a被遺忘後:theFoundCMD bat爲什麼%% 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 
) 
+2

不使用'goto'或標籤了'for'循環中。他們打破了循環(所以當然所有的循環變量都會丟失) – Stephan

回答

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 

(獎金:你不需要delayed expansion

相關問題