Find and Replace/Move DOS files - A handy DOS script
I did a simple google search, to find a script which will allow me to rename a bunch of files within a folder, but did not find anything! So - I made my own. Please feel free to do whatever you want with the below - just hope it helps you in some way.
A simple script I have put together to find files within a folder, and then rename/move them to an .old file. Best to run it from the dos prompt, and makes also a good idea to test it prior to running, such as against a bunch of test/dummy files as I would hate.
Below is the code - plus some documentation. At the end of this post - is a RAR file you can download, just in case you cant copy/paste the below for some reason.
Known limits: It does not seem to like wildcards at the moment, but I am sure it will be a simple fix. If you are using a folder which contains a space, enclose it in ” ’s so the CD command will work correctly.
@echo off
REM ** Script created by Alan Lee @ Elcom (www.elcom.com.au)
REM ** A simple way to scan a file, for XXX file - and move/rename it (well, you could adjust the below to do whatever you want really)
REM **
REM ** Please feel free to copy/adjust/do whatever you want with the below - all I can hope for is it will make your job a little easier!
REM **
REM ** Good luck! And as always, test in a non-production setup first to ensure it does what you want it to do! I take no responsibility etc for this script!
set drive=d:
set folder_to_scan=d:\xxxxx\xxxxxx\xxxxxxx
set file_to_find=xxxxxxxxx.doc
REM ** Three things you need to adjust …
REM ** drive = the actual drive which contains the files you wish to move/replace (the script, as below, does a drive: to get
REM ** to the drive in the event you run this script from a different drive to begin with (ie, you might have a scripts folder on C: for example)
REM **
REM ** folder_to_scan = the actual folder the script will chdir into, and search
REM **
REM ** file_to_find = the actual file we wish to find / replace. by default, as per the script below - it will rename/move this to %file_to_find%.old
%drive%
cd %folder_to_scan%
REM ** Goto the drive, CD into the foler
dir %file_to_find% /b /s > tmp.txt
REM ** Do a dir and store the results into a tmp file (called tmp.txt)
notepad tmp.txt
pause
REM ** Uncomment the above two lines, to add a little more info to what the script does (ie, see the files it has found).
REM You can then control+c / close the script window if it looks evil / found the wrong info!!
for /D %%I in (”%file_to_find%”) do For /F “tokens=*” %%J in (tmp.txt) do move “%%J” “%%J”.old && echo %%J
REM ** Do the actual work, scan the tmp.txt file, pull out the information - and do the move/rename to .old
del tmp.txt
REM ** Delete the tmp file
You can download the files from the below link
http://www.alanjlee.com/downloads/FindAndReplaceFiles.rar
Oh, and a slightly changed version which actually copies a new file over the found file (ie, mass replace)
http://www.alanjlee.com/downloads/FineAndReplaceFilesWithNewFile.rar
