我遇到的问题是write a batch file to get a directory of text files/ search for a specific token and insert a line after that中已解决问题的一个子集。
我需要从文件夹中读取一堆文本文件。为每个文本文件找到特定的标记,例如“Corolla”,并在标记之后插入带有变体的新行。假设文本文件如下:
first line
second line
a part of third line, Corolla, third line remaining
INSERT THE NEW LINE Number 1 HERE
a part of forth line, Corolla, firth line remaining
INSERT THE NEW LINE Number 2 HERE
The rest of file.
并对文件夹中的其他文件执行此操作。另外,我不希望为每个文件重置变体。假设在第二个文件中找到了Corolla,因此变体应该从3开始。
非变体版本的解决方案是:
@echo off
set SrcFolder=C:\Input
set DstFolder=C:\Output
for %%a in ("%SrcFolder%\*.txt") do (
(for /f "usebackq delims=" %%h in ("%%a") do (
echo.%%h
echo %%h|findstr "Corolla" >nul&if not errorlevel 1 (
echo THE NEW LINE
)
))>"%DstFolder%\%%~nxa"
)
但我也需要添加变体。
由于
答案 0 :(得分:1)
@echo off
set SrcFolder=C:\Input
set DstFolder=C:\Output
set Variant=0
for %%a in ("%SrcFolder%\*.txt") do (
(for /f "usebackq delims=" %%h in ("%%a") do (
echo.%%h
echo %%h|findstr "Corolla" >nul&if not errorlevel 1 (
set /a "Variant+=1"
setlocal enabledelayedexpansion
echo THE NEW LINE !Variant!
endlocal
)
))>"%DstFolder%\%%~nxa"
)