Windows 脚本:批量替换项目文件中的指定内容
以下是使用 Windows 脚本遍历项目所有文件,查找与参考文件.txt 内容匹配的行,并将相同的行替换为指定内容的示例代码:
@echo off
setlocal enabledelayedexpansion
set "reference_file=参考文件.txt"
set "replacement_content=指定内容"
for /r %%G in (*) do (
if not "%%~nxG"=="%reference_file%" (
set "temp_file=%%G.tmp"
findstr /n "^" "%%G" > "!temp_file!"
findstr /f:"%reference_file%" "!temp_file!" > nul && (
echo Found matching lines in "%%~nxG"
for /f "usebackq delims=:" %%H in ("!temp_file!") do (
set /a line_number=%%H-1
(for /f "tokens=1* delims=:" %%I in ('findstr "^" "%%G"') do (
if %%I equ !line_number! (
echo %replacement_content%
) else (
echo %%J
)
)) > "%%G.tmp"
move /y "!temp_file!" "%%G" > nul
)
)
del "!temp_file!" > nul
)
)
endlocal
请将代码中的参考文件.txt替换为您实际的参考文件的名称,将指定内容替换为您想要替换行的内容。
此脚本将遍历当前目录及其子目录中的所有文件(除了参考文件本身),使用findstr命令查找与参考文件中的内容匹配的行,并将其替换为指定内容。替换后的文件将保留原始文件的名称和路径。
请注意,脚本将直接修改原始文件,请务必备份重要文件或在测试环境中使用。
原文地址: https://www.cveoy.top/t/topic/pCee 著作权归作者所有。请勿转载和采集!