Recursively find and replace files

丶灬走出姿态 提交于 2019-12-03 10:09:20

The Batch file below start from current directory, recursively search the file given in the first parameter and copy over it (with same name) the file given in second parameter:

@echo off
set targetName=%~NX1
set replacementFile=%~F2
call :processFolder
goto :EOF

:processFolder
rem For each folder in this level
for /D %%a in (*) do (
   rem Enter into it, process it and go back to original
   cd %%a
   if exist "%targetName%" (
      copy "%replacementFile%" "%targetName%" /Y
   )
   call :processFolder
   cd ..
)
exit /B

For example:

app test1.txt c:\data\replacementfile.txt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!