Windows批处理嵌套for循环问题

时间:2014-11-04 09:51:31

标签: windows batch-file

我需要逐行读取外部循环中的文件,取这个值并在内部循环中使用它。但是目前我能够从这个文件中读取第一行并在内部循环中执行一些必需的处理,但外部循环只运行一次。

为什么外循环只运行一次?

myfile.txt包含:

AWC00201
AWC00202
AWC00203
DDDD

@echo off
setlocal EnableDelayedExpansion
for /F %%D in (myfile.txt) do (  
echo %D%
S:
cd \@vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D
echo %%D
FOR /F %%i IN ('dir /b /ad-h /o-d') DO (
echo After Nested For
echo %%D
SET test=%%D
SET b=%%i
GOTO found
)
echo No subfolder found
goto done
:found
echo %D%
echo Most recent subfolder: %b%
cd %b%
echo %%D
find /c "O K" tooling.report
echo %D%
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
echo %D%
echo now go up
echo !test!
echo %test%
)
pause

我得到以下输出:

ECHO is off.
AWC00201
After Nested For
AWC00201
ECHO is off.
Most recent subfolder: 20141103_170658_wro_awc
%D
____________ TOOLING.REPORT:  0
ECHO is off.
notfound
ECHO is off.
now go up
AWC00201
AWC00201
Press any key to continue . . .

1 个答案:

答案 0 :(得分:1)

您的代码有一个大问题,有一件事要改变

问题是在goto循环内部不能使用for并保持循环迭代。 goto取消for循环播放。

要改变的是你对变量的使用。您可以在for可替换参数中获得所需的信息。使用它们。当可替换参数不能满足您的需要时,将值移动到变量,但情况并非如此

@echo off
setlocal enableextensions disabledelayedexpansion

for /F "delims=" %%D in (myfile.txt) do (

    cd /d "s:\@vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D"
    for /d %%a in (.) do echo Current folder is "%%~fa"

    set "file="
    FOR /F "delims=" %%i IN ('dir /b /ad-h /o-d 2 >nul ') DO if not defined file (
        set "file=1"
        echo subfolder found : %%i
        find /c "O K" ".\%%i\tooling.report" >nul 2>nul 
        if errorlevel 1 (
            echo O K found 
        ) else (
            echo O K not found or file does not exist
        )
    )
    if not defined file (
        echo subfolder not found
    )
)
pause