我有一个包含很多子目录的文件夹,每个子目录都有一个 .diff 文件,我想使用批处理脚本在该文件中执行特定字符串的搜索。由于我们的 IT 要求,我无法访问 powershell。我试图在 .diff 文件中搜索特定字符串,并总结是否找到它或不是摘要文件。这部分我正在使用此脚本进行工作,感谢该网站上的另一位用户:
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="
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 (
if not defined matchingLine set "matchingLine=%%a"
)
if defined matchingLine (
echo %%~nxf, Failed >> "%outputFile%"
) else (
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="
set "startLine="
set "endLine="
REM Read each line from the file and process content between searchString and endString
for /f "tokens=1,* delims=" %%a in ('findstr /nilbc:" COMPARE DIFFERENCE FOUND AT" "%%f"') do (
if not defined startLine set "startLine=%%a" & set "matchingLine=%%b"
)
for /f "tokens=1,* delims=" %%c in ('findstr /nilbc:" BOTTOM OF GOOD FILE" "%%f"') do (
if not defined endLine set "endLine=%%c"
)
if defined matchingLine (
echo %%~nxf, Failed >> "%outputFile%"
for /f "tokens=1,* delims=" %%L in ("%%f") do (
set LINE=%%L
if !LINE!=="!endLine!" (set start=0)
if !start! equ 1 (echo !line! >> "%output_fail%")
if !LINE!=="!startLine!" (set start=1)
)
) else (
echo %%~nxf, Passed >> "%outputFile%"
)
)
我认为我误解了 %% 变量,并且不知道如何纠正这个问题。
您的第一个问题“查找某行的行号”具有误导性。
当然,你可以找到起始字符串和结束字符串的行号,然后用“如果当前行号在起始字符串和结束字符串之间,则打印该行”重新处理文件,但这意味着要多次读取每个文件,这很耗时。
最好只解析每个文件一次,在到达起始行时开始打印,在到达结束行时停止打印。我使用标志来跟踪(某种“二进制变量” - 定义或未定义 - 定义时它们的实际值并不重要) -
flag
来跟踪我们是否在所需的文本块内,found
来了解我们是否在所需的文本块内已经找到了文本块,因此我们只能输出第一次出现的情况并忽略任何其他出现的情况。当文件完全处理完毕并且 found
仍未定义时,我们知道没有匹配的文本块。
@echo off
setlocal enabledelayedexpansion
set "folderpath=."
(FOR /r "%folderPath%" %%f in (*.diff) do (
REM Initialize variables
set "flag=
set "found="
REM Read each line from the file and process content when between searchString and endString
for /f "usebackq tokens=* delims=" %%a in ("%%f") do (
if not defined found (
echo %%a|findstr /ilbc:" COMPARE DIFFERENCE FOUND AT" >nul 2>&1 && (
set "flag=1"
echo %%~nxf, Failed
)
if defined flag if not defined found echo %%a
echo %%a|findstr /ilbc:" BOTTOM OF GOOD FILE" >nul 2>&1 && (
set "flag="
set "found=1"
)
)
)
if not defined found echo %%~nxf, Passed
echo\
))>"%outfile%"
注意:
for /f
循环会忽略空行和以;
开头的行。|<>&!%
)也会产生问题(语法错误)
我(暂时)没有处理这个问题,以使其(相对)易于理解。