Skip to content

Instantly share code, notes, and snippets.

@lukaszjablonski
Last active March 17, 2022 11:37
Show Gist options
  • Save lukaszjablonski/e2b6b38552f8a21bef1b3bc48b7eba00 to your computer and use it in GitHub Desktop.
Save lukaszjablonski/e2b6b38552f8a21bef1b3bc48b7eba00 to your computer and use it in GitHub Desktop.
Recursively find and replace files (Windows)
rem Recursively find and replace files
rem This script starts from current directory recursive search for the file name given in the first parameter and replace it (keeping its name) with the file given in second parameter.
rem source: https://stackoverflow.com/a/10423757
rem usage: fr.bat targetName replacementFile
rem example fr.bat test1.txt c:\data\replacementfile.txt would recursively search for "test1.txt" and replace it (keeping its name) withe the file "c:\data\replacementfile.txt"
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment