# BAT file/Script to find and replace files



## NinjaNife (May 23, 2011)

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


----------



## streetfighter 2 (May 23, 2011)

This should do it, though it's probably not the cleanest method.  Tests alright on my computer.

```
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:

```
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_:

```
@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:

```
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.
```


----------



## NinjaNife (May 23, 2011)

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"?


----------



## streetfighter 2 (May 23, 2011)

NinjaNife said:


> 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:

```
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:

```
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:

```
subupdate.bat test.xml
```


----------



## NinjaNife (May 23, 2011)

Works perfectly (second one for bat file)!  Thank you   Saved me a lot of hair


----------



## Lucasonas (Apr 13, 2012)

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


----------



## FordGT90Concept (Apr 13, 2012)

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)?


----------



## streetfighter 2 (Apr 13, 2012)

My batch skills are a bit rusty.  GNU tools are so much better .

This might work if the files are all in the same directory:

```
@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 (Apr 13, 2012)

NinjaNife said:


> 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 

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


----------



## Lucasonas (Apr 14, 2012)

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!


----------



## Kreij (Apr 14, 2012)

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.  (.. and Welcome to TPU !!)


----------



## Lucasonas (Apr 14, 2012)

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?


----------



## FordGT90Concept (Apr 14, 2012)

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.


----------



## leeuniverse (Nov 4, 2014)

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.


----------

