Variables 提取周期批处理文件中Findstr条件下的行
我正试图从一组txt文件中提取行 在一个匹配的搜索条件下放置两行 并将输出重定向到csv文件 我成功地为集合中的特定txt文件执行了该操作, 但我在为《循环》写文章时遇到了麻烦 批量扫描给定文件夹中的每个txt 通过,我编写了以下代码来扫描 特定的文件。它工作得很好:Variables 提取周期批处理文件中Findstr条件下的行,variables,batch-file,for-loop,findstr,Variables,Batch File,For Loop,Findstr,我正试图从一组txt文件中提取行 在一个匹配的搜索条件下放置两行 并将输出重定向到csv文件 我成功地为集合中的特定txt文件执行了该操作, 但我在为《循环》写文章时遇到了麻烦 批量扫描给定文件夹中的每个txt 通过,我编写了以下代码来扫描 特定的文件。它工作得很好: setlocal EnableDelayedExpansion cd "myFolder" if exist myOutput.csv del myOutput.csv for /F "delims=:" %%A in ('fi
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "myFile.txt"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
)
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "myFile.txt" ^| findstr /B "%LineBelow%"') do ^
echo %%B>>myOutput.csv)
start myOutput.csv
ENDLOCAL
当我尝试在For循环中概括代码以扫描myFolder中的每个txt时
我在Findstr中出错:!下线!正好是一个空变量
以下是有缺陷的For循环:
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for %%F IN ("*.txt") do (
(
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "%%F"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
))
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "%%F" ^| findstr /B "!LineBelow!"') do ^
echo %%B>>myOutput.csv))
start myOutput.csv
ENDLOCAL
有人能帮我改正这个密码吗?
谢谢@echo off
setlocal enableextensions enabledelayedexpansion
“我的文件夹”光盘
>myOutput.csv(
对于/f“tokens=1,2 delims=:”%%a in('
findstr/b/n/c:“我的标准”*.txt
""做"(
设置/a“行=%%b+2”
对于/f“tokens=1,*delims=:”%%c in('
findstr/n“^”“%a”^ findstr/b/c:“!line!:”2^>nul
“)做回显(%%d
)
)
这使用findstr
直接枚举与myCriteria匹配的所有行,并在输出中包括文件名(%%a
)和行号(%%b
)。此信息用于从匹配文件中检索最后一行(%%d
)