foundkey
12/29/2018 - 6:50 AM

RepalceText

Replace all files in folders

::用于替换文件夹下的文本文件
::2018-12-29
::FK

@echo off
setlocal enabledelayedexpansion


set InDir=%~1
set OutDir=%~1_replaced

echo only support text file

::获取信息 
set /p Type= replace file extension:
set /p Ori= original content:
set /p Replace= replace content:

::遍历文件夹
for /f %%i in ('dir /b /s %InDir%\*.%Type%') do (
    set curFile=%%i
    set newFile=!curFile:%InDir%=%OutDir%!
    
    ::创建保存替换文件的路径
    set curPath=%%~dpi
    set newPath=!curPath:%InDir%=%OutDir%!
    if NOT exist !newPath! mkdir !newPath!
    
    ::逐行替换文件
    for /f "delims=" %%j in ('type !curFile!') do (
        set curLine=%%j
        set "curLine=!curLine:%Ori%=%Replace%!"
        echo !curLine!>>!newFile!
    )
)

pause