用win脚本遍历项目所有文件查找项目所有文件的行内容如果与参考文件txt中的行相同就替换成指定的内容
以下是一个示例的Windows脚本,可以用于遍历项目中的所有文件,查找项目所有文件的行内容,并根据参考文件.txt中的行相同与否进行替换。
@echo off
setlocal enabledelayedexpansion
set "reference_file=参考文件.txt"
set "replacement_content=替换的内容"
for /r %%F in (*) do (
if not "%%~nxF"=="%~nx0" (
echo Processing file: %%F
set "temp_file=%%~dpFtemp.txt"
findstr /n "^" "%%F" > "!temp_file!"
for /f "delims=: tokens=1,*" %%A in ('type "!temp_file!"') do (
set "line=%%B"
findstr /x "!line!" "%reference_file%" >nul
if !errorlevel! equ 0 (
echo Replacing line: "!line!" in file: %%F
echo %replacement_content%>>"%%F"
)
)
del "!temp_file!"
)
)
endlocal
请注意,上述脚本会将匹配到的行附加到文件的末尾,而不是替换原始行。如果您希望替换原始行,请将echo %replacement_content%>>"%%F"改为echo %replacement_content%>temp.txt & move /y temp.txt "%%F"。
另外,请确保将“参考文件.txt”替换为您实际的参考文件名,并将“替换的内容”替换为您希望用于替换的内容
原文地址: https://www.cveoy.top/t/topic/hTaV 著作权归作者所有。请勿转载和采集!