1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

BAT file/Script to find and replace files

Discussion in 'Programming & Webmastering' started by NinjaNife, May 23, 2011.

  1. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.08/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    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
     
  2. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (0.94/day)
    Thanks Received:
    732
    Location:
    Philly
    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: May 23, 2011
    NinjaNife says thanks.
  3. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.08/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    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"?
     
  4. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (0.94/day)
    Thanks Received:
    732
    Location:
    Philly
    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
     
    Jizzler and NinjaNife say thanks.
  5. NinjaNife New Member

    Joined:
    Oct 15, 2009
    Messages:
    172 (0.08/day)
    Thanks Received:
    8
    Location:
    Kentucky, USA
    Works perfectly (second one for bat file)! Thank you :D Saved me a lot of hair ;)
     
    streetfighter 2 says thanks.
  6. Lucasonas New Member

    Joined:
    Apr 13, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    Location:
    CA
    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:
     
  7. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,880 (6.16/day)
    Thanks Received:
    4,534
    Location:
    IA, USA
    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)?
     
    Crunching for Team TPU
  8. streetfighter 2

    streetfighter 2 New Member

    Joined:
    Jul 26, 2010
    Messages:
    1,658 (0.94/day)
    Thanks Received:
    732
    Location:
    Philly
    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%
     
    Lucasonas says thanks.
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.58/day)
    Thanks Received:
    5,623
    Location:
    Cheeseland (Wisconsin, USA)
    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:
     
  10. Lucasonas New Member

    Joined:
    Apr 13, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    Location:
    CA
    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: Apr 14, 2012
  11. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.58/day)
    Thanks Received:
    5,623
    Location:
    Cheeseland (Wisconsin, USA)
    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 says thanks.
  12. Lucasonas New Member

    Joined:
    Apr 13, 2012
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    Location:
    CA
    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: Apr 15, 2012
  13. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,880 (6.16/day)
    Thanks Received:
    4,534
    Location:
    IA, USA
    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.
     

    Attached Files:

    Lucasonas says thanks.
    Crunching for Team TPU
  14. leeuniverse New Member

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

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page