Translate

Friday, June 13, 2014

Windows batch file to replace all occurrences of a file

You can use the batch file below to loop through all the folders inside
 C:\my destination and replace any occurrences of a.txt with the one from
 C:\my source\a.txt

To use the batch file just replace the values of the variables in red

I have tested this in windows 7

@echo off
set sourceFileFolder=C:\my source
set sourceFileName=a.txt
set ReplaceInChildFoldersOf=C:\my destination

REM DO NOT MODIFY ANYTHING BELOW THIS LINE!!

set sourceFileName=%sourceFileName: =%
set fullSource="%sourceFileFolder%\%sourceFileName%"
set ReplaceInChildFoldersOf="%ReplaceInChildFoldersOf%\"


echo Replacing all occurrences of %sourceFileName%
echo in %ReplaceInChildFoldersOf%
echo with the one in %fullSource%
echo Do you wish to continue?

pause

FOR /R %ReplaceInChildFoldersOf% %%I IN (%sourceFileName%)  DO if exist "%%~fI" COPY /Y %fullSource% "%%~fI" 

pause
                                                                                                

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator