Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 07, 2016, 02:20:10 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: replace files in batch  (Read 995 times)

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,473
    • View Profile
    • Donate to Member
replace files in batch
« on: October 22, 2012, 05:07:59 PM »
hello!

I have a directory, where there are many files with the same filename, inside various subdirectories

I want to replace each of these files, with a specific file I have

how can I do this at once? any solution?

thanks!

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,666
    • View Profile
    • Donate to Member
Re: replace files in batch
« Reply #1 on: October 22, 2012, 08:18:13 PM »
This is easy to do but there is one caveat.  Do you want to replace the "contents" of these filenames with your specific file or do you want to replace the entire file with a new file.  For example:

Assume the files that need replacing are named File.txt.  Assume the file you want to use as a replacement is named NewFile.txt.

1) Did you want to replace the contents of File.txt with the contents of NewFile.txt (thus leaving the original File.txt name in place)?

2) Or, did you want to completely replace File.txt with NewFile.txt.

Of course, this scenario is moot if you're looking to replace File.txt with a completely different file that is also named File.txt.

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,473
    • View Profile
    • Donate to Member
Re: replace files in batch
« Reply #2 on: October 23, 2012, 07:46:14 AM »
the 2)

I want to  replace the whole files File.txt with NewFile.txt

but the source file (the file that will replace the others) is named File.txt as well

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 4,666
    • View Profile
    • Donate to Member
Re: replace files in batch
« Reply #3 on: October 23, 2012, 08:38:26 AM »
Here's an AuotHotkey snippet that should do it (adjust variables as necessary).  However, run it on a test folder structure first.

Code: Autohotkey [Select]
  1. myDir := "c:\tmp6"                 ; Will be recursed.
  2. myFileName := "File.txt"           ; Filename to look for.  Name only, no path.
  3. myReplacementFile := "c:\File.txt" ; Full path to replacement file.
  4.  
  5. Loop, % myDir . "\*.*", 0, 1
  6. {
  7.     If ( A_LoopFileName = myFileName )
  8.     {
  9.         FileCopy, % myReplacementFile, % A_LoopFileFullPath, 1
  10.     }
  11. }

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,473
    • View Profile
    • Donate to Member
Re: replace files in batch
« Reply #4 on: October 23, 2012, 08:41:46 AM »
it does the job, thanks!