将AAA文件里全部子文件中包含aaa bbb ccc 的内容分别替换为eee fff ggg
点击查看代码
chcp 65001
@echo off
setlocal enabledelayedexpansion
:: 创建一个临时文件来保存包含目录列表的文件名
dir /b /ad "AAA" > temp_dirs.txt
:: 检查临时文件是否存在
if not exist temp_dirs.txt (
echo No directories found in "AAA".
goto end
)
:: 遍历每个子目录
for /f "delims=" %%d in (temp_dirs.txt) do (
echo Processing directory: %%d
:: 构造子目录的完整路径
set "dir_path=AAA\%%d"
:: 使用PowerShell替换子目录中所有文件的内容
powershell -Command "$dir_path = $env:PS_DIR_PATH; Get-ChildItem -Path $dir_path -Recurse -File | ForEach-Object { (Get-Content $_.FullName -Encoding UTF8) -replace 'aaa', 'eee' | Set-Content $_.FullName -Encoding UTF8 }"
powershell -Command "$dir_path = $env:PS_DIR_PATH; Get-ChildItem -Path $dir_path -Recurse -File | ForEach-Object { (Get-Content $_.FullName -Encoding UTF8) -replace 'bbb', 'fff' | Set-Content $_.FullName -Encoding UTF8 }"
powershell -Command "$dir_path = $env:PS_DIR_PATH; Get-ChildItem -Path $dir_path -Recurse -File | ForEach-Object { (Get-Content $_.FullName -Encoding UTF8) -replace 'ccc', 'ggg' | Set-Content $_.FullName -Encoding UTF8 }"
)
:end
:: 删除临时文件
del temp_dirs.txt
echo Done.
pause