批处理文本替换脚本

功能:将AAA文件里全部子文件中包含aaa bbb ccc 的内容分别替换为eee fff ggg
使用说明:记事本复制下面code,修改文件后缀为.bat,将脚本放置在同AAA文件所在目录下,双击运行即可

点击查看代码
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  
posted @   optimus_leo  阅读(63)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示