• We've upgraded our forums. Please post any issues/requests in this thread.

BAT file/Script to find and replace files

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.06/day)
Likes
8
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
#1
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
 
Joined
Jul 26, 2010
Messages
1,655 (0.61/day)
Likes
729
Location
Philly
System Name Primary Rig
Processor Phenom II X4 B50 @ 3.7GHz
Motherboard Biostar TA790GX 128M
Cooling Sunbeam CR-CCTF 120mm , 6x120mm, MOS-C1
Memory 2x2GB Kingston HyperX 1066 @ 800 4-4-4-12
Video Card(s) Sapphire HD 5830 800/1000 @ 885/1225
Storage 320GB, 400GB, 500GB, 1.5TB
Display(s) Hannspree HF259
Case CM 690
Power Supply OCZ 850W
Benchmark Scores 3Dmark06: 18545/5219 CPU Mark 7.0: 3911.2 Cinebench R10: 11826/3359 x264 HD 2.0: 75.6/23.9
#2
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:

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.06/day)
Likes
8
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
#3
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"?
 
Joined
Jul 26, 2010
Messages
1,655 (0.61/day)
Likes
729
Location
Philly
System Name Primary Rig
Processor Phenom II X4 B50 @ 3.7GHz
Motherboard Biostar TA790GX 128M
Cooling Sunbeam CR-CCTF 120mm , 6x120mm, MOS-C1
Memory 2x2GB Kingston HyperX 1066 @ 800 4-4-4-12
Video Card(s) Sapphire HD 5830 800/1000 @ 885/1225
Storage 320GB, 400GB, 500GB, 1.5TB
Display(s) Hannspree HF259
Case CM 690
Power Supply OCZ 850W
Benchmark Scores 3Dmark06: 18545/5219 CPU Mark 7.0: 3911.2 Cinebench R10: 11826/3359 x264 HD 2.0: 75.6/23.9
#4
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
 

NinjaNife

New Member
Joined
Oct 15, 2009
Messages
172 (0.06/day)
Likes
8
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
#5
Works perfectly (second one for bat file)! Thank you :D Saved me a lot of hair ;)
 

Lucasonas

New Member
Joined
Apr 13, 2012
Messages
3 (0.00/day)
Likes
0
Location
CA
#6
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:
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,922 (6.24/day)
Likes
10,022
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#7
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)?
 
Joined
Jul 26, 2010
Messages
1,655 (0.61/day)
Likes
729
Location
Philly
System Name Primary Rig
Processor Phenom II X4 B50 @ 3.7GHz
Motherboard Biostar TA790GX 128M
Cooling Sunbeam CR-CCTF 120mm , 6x120mm, MOS-C1
Memory 2x2GB Kingston HyperX 1066 @ 800 4-4-4-12
Video Card(s) Sapphire HD 5830 800/1000 @ 885/1225
Storage 320GB, 400GB, 500GB, 1.5TB
Display(s) Hannspree HF259
Case CM 690
Power Supply OCZ 850W
Benchmark Scores 3Dmark06: 18545/5219 CPU Mark 7.0: 3911.2 Cinebench R10: 11826/3359 x264 HD 2.0: 75.6/23.9
#8
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%
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#9
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:
 

Lucasonas

New Member
Joined
Apr 13, 2012
Messages
3 (0.00/day)
Likes
0
Location
CA
#10
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:

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#11
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 !!)
 

Lucasonas

New Member
Joined
Apr 13, 2012
Messages
3 (0.00/day)
Likes
0
Location
CA
#12
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:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
20,922 (6.24/day)
Likes
10,022
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#13
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

Joined
Nov 4, 2014
Messages
1 (0.00/day)
Likes
0
#14
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: