• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

BAT file/Script to find and replace files

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.03/day)
Location
Kentucky, USA
System Name Custom
Processor Intel i7-875K 2.9GHz (OC'd to 4.0GHz)
Motherboard MSI P55-GD80
Cooling Sidewinder Custom MCP655 / Watercool HEATKILLER® CPU Rev3.0 1156 LT / Swiftech MCR320-QP / XSPC Dual
Memory G.SKILL PiS 2000MHz 8GB 6-9-6-24
Video Card(s) EVGA GTX 470 1280MB Superclocked+ 750/1500/1750
Storage Western Digital Black x2 1TB RAID-0, Seagate Barracuda 1TB, Western Digital 500GB
Display(s) ASUS VH242H 24" 1080p
Case Cooler Master HAF 932
Audio Device(s) Creative X-FI Titnium Fatal1ty Champion Series
Power Supply Corsair Professional Series AX1200 1200W
Software Windows 7 Ultimate nVidia Edition 64bit
Hey guys. Not sure if this is the correct forum, since this doesn't have to do with HTML or anything, so if it is in the wrong place please let me know (or move it if you can).

I am looking for a BAT file or script (preferably BAT file) that can find files with a specific name and overwrite them with one example file (located in the same folder as the BAT file). Here is an example of what I mean:

There are 10 files, all located in different folders in the same tree and its branches. At the top, there is "File_A", and below it are "File_A" copies, but they are older versions (same file name, different data in the file). I need the something that will search all the sub-directories, find all of the copies of "File_A", and replace it with the new version ("File_A" located at the top of the tree).

Wow, that made a lot more sense before I tried typing it lol. If you have any questions I will do my best to answer them, but I don't have much experience with this stuff.. Any help would be greatly appreciated. Thanks :)


Brendan "NinjaNife" West
 
This should do it, though it's probably not the cleanest method. Tests alright on my computer.
Code:
for /F "tokens=*" %P in ('dir /b /s myfile.txt') do copy /Y .\myfile.txt "%P"

If you're running it from a bat file use %%P instead of %P.

EDIT: I corrected the command to work with file paths that have spaces (notice the quotes around the last parameter). Also when running the command you'll always get this error:
Code:
The file cannot be copied onto itself.
Just ignore that, because it doesn't hurt anything.

Here's a batch file version that I called subupdate.bat:
Code:
@echo off
if "%1"=="" GOTO End
for /F "tokens=*" %%P in ('dir /b /s %1') do copy /Y .\%1 "%%P"
:End
Then you can invoke it as such:
Code:
C:\root_dir_for_changes\subupdate.bat my_file_to_update.txt
The file cannot be copied onto itself.
        0 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
 
Last edited:
Thanks for your help. Just one question.. Where do I input the file name? For instance, what would the code look like if the file I wanted to copy/replace was named "test.xml"?
 
For instance, what would the code look like if the file I wanted to copy/replace was named "test.xml"?
Depends how you're running the command. If you're running it from the command prompt then CD into the root directory containing the most recent "test.xml". Then run the following command:
Code:
for /F "tokens=*" %P in ('dir /b /s test.xml') do copy /Y .\test.xml "%P"

If you're running it from a bat file then the bat file should contain:
Code:
for /F "tokens=*" %%P in ('dir /b /s test.xml') do copy /Y .\test.xml "%%P"

If you're using my subupdate.bat described in my first post then CD into the root directory containing the most recent "test.xml". Then run the following command:
Code:
subupdate.bat test.xml
 
Works perfectly (second one for bat file)! Thank you :D Saved me a lot of hair ;)
 
Hi, innovative thinkers! I am looking for a BAT file or script that takes random named zips, consisting of 5 random numerals in the name, and renames the zips identical to different .txt files with that same 5 numeral in the same folder.

Here is an example of what I mean:

BEFORE BAT

C:\Documents

45678_Example_720p.zip
34284_Example_720p.zip
67483_Example_720p.zip

RAT_Example_45678_24f_ONO.txt
RAT_Example_34284_24f_ONO.txt
RAT_Example_67483_24f_ONO.txt

AFTER BAT

C:\Documents

RAT_Example_45678_24f_ONO.zip
RAT_Example_34284_24f_ONO.zip
RAT_Example_67483_24f_ONO.zip

RAT_Example_45678_24f_ONO.txt
RAT_Example_34284_24f_ONO.txt
RAT_Example_67483_24f_ONO.txt

I would like the BAT to take only those matching 5 numerals as a reference.

Thanks

:ohwell:
 
So you have a directory containing both random named ZIP files and consistently named TXT files and you want to apply the TXT file naming patern to the ZIP files where the 5-digit numbers match? Are there sub-directories? Are all the ZIP file names in that pattern (#####_Example_720p.zip) and all the TXT files in that pattern (RAT_Example_#####_24f_ONO.txt)?
 
My batch skills are a bit rusty. GNU tools are so much better :D.

This might work if the files are all in the same directory:
Code:
@echo off
for /F "tokens=*" %%P in ('dir /b "*.zip"') do (
	call :RENAME %%P
)

:RENAME
set oldname=%*
dir /b "*.txt" | find "%oldname:~0,5%" > %TEMP%\newname.txt
set /p newname= < %TEMP%\newname.txt
REM Test this before you remove echo!
echo move %oldname% %newname:.txt=.zip%
 
Hey guys. Not sure if this is the correct forum, since this doesn't have to do with HTML or anything

Hey Ninja,
The Programming and Webmastering (P&W) section is for anything related to any programming or scripting languages, and anything to do with managing/coding websites and pages.

Except COBOL. No COBOL allowed in P&W :laugh:

Feel free to post any script related questions. When people ask, everyone learns (or at least gets a quick refresher course) :toast:
 
Greetings, streetfighter 2.

I'll give your BAT a try. Thanks so much.

Greetings, FordGT90Concept!

Question:

"So you have a directory containing both random named ZIP files and consistently named TXT files and you want to apply the TXT file naming patern to the ZIP files where the 5-digit numbers match?"

Answer: Yes

Question:

Are there sub-directories?

Answer: No

Question:

"Are all the ZIP file names in that pattern (#####_Example_720p.zip) and all the TXT files in that pattern (RAT_Example_#####_24f_ONO.txt)?"

Answer: Yes

Sorry about the terse replies, but your right: "Never assume."

Greetings, Streetfighter 2

I'll give your BAT a try. Thanks alot!
 
Last edited:
Hi Lucas,
I deleted your duplicate post and merged the next two.
Please use the edit function to add to a post instead of double posting.
Thanks. :toast: (.. and Welcome to TPU !!)
 
FordGT90Concept!!!

Your attachment is very close, it kept all the 5 numerals intact, but like you said: "it will rename them all from "#_Example_720p.zip" to "RAT_Example_#_24f_ONO.zip"...

Greetings, Streetfighter 2

I can't seem to get the BAT to work, but I'll still keep on trying.


Any other suggestions, or BAT scripts Streetfighter & FordGT90Concept?
 
Last edited:
Place the attached executable in the directory containing the zip files and run it. It will rename them all from "#_Example_720p.zip" to "RAT_Example_#_24f_ONO.zip"...

It's not very intelligent so that's literally all it can do.
 

Attachments

Hey all....

Can someone help me make the BAT script in the 2nd post above to be able to use MORE than "1" file?
As it currently is, you can only replace one particular file.

For example, I want in the command line to be able to write two files or more, and the script will find and replace all those 2 or more named files.
Thanks much.

P.S.... Is there a GUI that does file searching and replacing?
I've spent hours searching on the net, and the only thing I can find are "text" search/replace and this BAT command, not actual "file" replacing. I find this amazing. :(
 
Last edited:
Back
Top