找到字符串的第一个实例时停止内部循环的批处理脚本

问题描述 投票:0回答:1

我有一个包含很多子目录的文件夹,每个子目录都有一个 .diff 文件,我想使用批处理脚本在该文件中执行特定字符串的搜索。由于我们的 IT 要求,我无法访问 powershell。一旦在文件中找到我的特定字符串(可以显示多次),我就尝试存在我的循环,并继续到下一个子目录。我尝试了一些不同的方法,但没有一个能够准确地满足我的需求。

以下脚本可以运行一点,但会再次重新启动外部循环(继续在相同的子目录中查找:

setlocal enabledelayedexpansion

REM Set the path to the folder containing subdirectories
SET folderPath=%~dp0

REM Set the specific string to search for at the beginning of a line
SET "searchString"="COMPARE DIFFERENCE FOUND AT"

REM Set the next string to search for indicating the end of content to copy
SET "endString"="EXTRA DATA SKIPPED ON TEST FILE"

REM Set the path for the output file to save the result
SET "outputFile=%folderPath%ValidationSummary.csv"
SET "output_fail=%folderPath%Validation_Fail.out"

REM Initialize a variable to track whether the start search string is found
SET "startStringFound=0"

REM Clear output file for first run
ECHO Run Name, Status > "%outputFile%"
TYPE NUL > "%output_fail%"

REM Loop through each subdirectory in the specified folder
FOR /r "%folderPath%" %%f in (*.diff) do (
    REM Initialize variables
    set "found=0"
    set "copying=0"
    set "matchingLine=0"

    REM Read each line from the file and process content between searchString and endString
    for /f "delims=" %%a in ('findstr /i /l /b /c:" COMPARE DIFFERENCE FOUND AT" "%%f"') do (
            set "matchingLine=%%a"
        set "found=1"
        set "copying=1"
        echo %%~nxf, Failed >> "%outputFile%"
        goto :continueSearch
        )
    if !found! equ 0 (
    echo %%~nxf, Passed >> "%outputFile%"
    ) else (
    continueSearch
    )
)

以下脚本可以工作,但每次它出现在文件中时它都会找到搜索字符串,但我想保留第一个实例的行号,这将覆盖它:

setlocal enabledelayedexpansion

REM Set the path to the folder containing subdirectories
SET folderPath=%~dp0

REM Set the specific string to search for at the beginning of a line
SET "searchString"="COMPARE DIFFERENCE FOUND AT"

REM Set the next string to search for indicating the end of content to copy
SET "endString"="EXTRA DATA SKIPPED ON TEST FILE"

REM Set the path for the output file to save the result
SET "outputFile=%folderPath%ValidationSummary.csv"
SET "output_fail=%folderPath%Validation_Fail.out"

REM Initialize a variable to track whether the start search string is found
SET "startStringFound=0"

REM Clear output file for first run
ECHO Run Name, Status > "%outputFile%"
TYPE NUL > "%output_fail%"

REM Loop through each subdirectory in the specified folder
FOR /r "%folderPath%" %%f in (*.diff) do (
    REM Initialize variables
    set "found=0"
    set "copying=0"
    set "matchingLine=0"

    REM Read each line from the file and process content between searchString and endString
    for /f "delims=" %%a in ('findstr /i /l /b /c:" COMPARE DIFFERENCE FOUND AT" "%%f"') do (
            set "matchingLine=%%a"
        set "found=1"
        set "copying=1"
        echo %%~nxf, Failed >> "%outputFile%"
        )
    if !found! equ 0 (
    echo %%~nxf, Passed >> "%outputFile%"
    )
)

以下脚本在第一次找到字符串时停止所有循环(因此不会继续搜索剩余的子目录)。

setlocal enabledelayedexpansion

REM Set the path to the folder containing subdirectories
SET folderPath=%~dp0

REM Set the specific string to search for at the beginning of a line
SET "searchString"="COMPARE DIFFERENCE FOUND AT"

REM Set the next string to search for indicating the end of content to copy
SET "endString"="EXTRA DATA SKIPPED ON TEST FILE"

REM Set the path for the output file to save the result
SET "outputFile=%folderPath%ValidationSummary.csv"
SET "output_fail=%folderPath%Validation_Fail.out"

REM Initialize a variable to track whether the start search string is found
SET "startStringFound=0"

REM Clear output file for first run
ECHO Run Name, Status > "%outputFile%"
TYPE NUL > "%output_fail%"

REM Loop through each subdirectory in the specified folder
FOR /r "%folderPath%" %%f in (*.diff) do (
    REM Initialize variables
    set "found=0"
    set "copying=0"
    set "matchingLine=0"

    REM Read each line from the file and process content between searchString and endString
    for /f "delims=" %%a in ('findstr /i /l /b /c:" COMPARE DIFFERENCE FOUND AT" "%%f"') do (
            set "matchingLine=%%a"
        set "found=1"
        set "copying=1"
        echo %%~nxf, Failed >> "%outputFile%"
        goto:ExitLoop
        )
    :ExitLoop
    if !found! equ 0 (
    echo %%~nxf, Passed >> "%outputFile%"
    )
)
for-loop batch-file batch-processing goto findstr
1个回答
0
投票

您不能在代码块/循环中使用标签 - 它往往会做非常不直观的事情,例如忽略以下命令等。并且您不能使用

goto
在代码块/循环中跳转。每个
goto
都会中断循环(所有循环),如果它引用任何元变量,这反过来会使以下代码无效
%%?
)

你的内循环可能看起来像:

    set "matchinLine="
    for /f "delims=" %%a in ('findstr /i /l /b /c:" COMPARE DIFFERENCE FOUND AT" "%%f"') do (
        if not defined matchingLine set "matchingLine=%%a"
    )
    if defined matchingLine (
       echo %%~nxf, Failed >> "%outputFile%"
    ) else (
       echo %%~nxf, Passed >> "%outputFile%"
    )

当您不需要实际找到的行时(您在问题中不会使用它),您甚至不需要内部

for
或任何变量(这意味着,您甚至不需要这部分代码的延迟扩展:

FOR /r "%folderPath%" %%f in (*.diff) do (
    findstr /i /l /b /c:" COMPARE DIFFERENCE FOUND AT" "%%f" && (
      echo %%~nxf, Failed >> "%outputFile%"
    ) || (
      echo %%~nxf, Passed >> "%outputFile%"
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.