Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
BeastOfExmoor
Aug 19, 2003

I will be gone, but not forever.
Humble Request
Problem:

I frequently need to replace one file that exists in many subfolders folders, with another file. Rather than manually searching for and replacing these files, I'd love a command line program/script that searches the subfolders for a specific filename, deletes that file, and then copies the new file into that folder.

Syntax like:
ReplaceFile oldfile.bin newfile.bin

Description and requirements:

Ideally this would be something that runs on bare Windows 10.

Nice to have features:
Ideally it would echo back the path of the directories where files have been replaced.

Adbot
ADBOT LOVES YOU

BeastOfExmoor
Aug 19, 2003

I will be gone, but not forever.
Sorry, I've been super busy for the last week and haven't had time to sit down and look at either of these. I really appreciate both of you taking time to write this silly time saver.

TheLastManStanding posted:

Are you replacing the file with one of the same name? Or do you want to set the name of the file you're looking to replace?

Request Fill
Edit: Threw something together in Autohotkey. For the source you'll need Autohotkey 1.1.* to run it and SciTE4AutoHotkey to edit. The program gives you a gui to select the new file and an edit box in case the old file has a different name. There is a checkbox for testing, allowing you to run it without actually replacing anything. A txt log is spit out at the end which lists all the files which were replaced. The script assumes that it's in the root directory to be searched.

Compiled
Source (ahk file)
Source (txt file)

This is an interesting approach. I use AutoHotKey very minimally, but never would have considered it for this task. Unfortunately I'm a little confused by how to use this script. When I open it, I get a GUI with a browse button which presumably is asking which file I want to replace. I don't know how I select the file I'd like to replace it with, however. And yes, the files will almost always be different filenames.


nielsm posted:

Alternative solution, in PowerShell.

Save this to a text file, e.g. Replace-FileByName.ps1:
code:
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
    [string]$SearchFileName,
    [string]$ReplacementFileName,
    [string]$SearchBase=".",
    [switch]$Rename
)
$originalFiles = Get-ChildItem -LiteralPath $SearchBase -Filter $SearchFileName -Recurse -File
foreach ($orgFile in $originalFiles) {
    Write-Verbose $orgFile.FullName
    if ($Rename) {
        Remove-Item -LiteralPath $orgFile.FullName
        Copy-Item -LiteralPath $ReplacementFileName -Destination $orgFile.DirectoryName
    } else {
        Copy-Item -LiteralPath $ReplacementFileName -Destination $orgFile.FullName
    }
}
Then you can call the script like this to replace oldfile.bin with newfile.bin, keeping the oldfile.bin filename:
pre:
.\Replace-FileByName.ps1 -SearchFileName oldfile.bin -ReplacementFileName d:\newfile.bin
If you'd rather the replaced files are called by the replaement's filename:
pre:
.\Replace-FileByName.ps1 -SearchFileName oldfile.bin -ReplacementFileName d:\newfile.bin -Rename
Or to check what will happen beforehand:
pre:
.\Replace-FileByName.ps1 -SearchFileName oldfile.bin -ReplacementFileName d:\newfile.bin -WhatIf
Or get it to print destination filenames before each replacement, and still perform all the replacements:
pre:
.\Replace-FileByName.ps1 -SearchFileName oldfile.bin -ReplacementFileName d:\newfile.bin -Verbose
You can also get it to confirm before each replacement: (But beware that the "Yes to all" and "No to all" choices don't work due to the foreach loop. I don't know what the best way to get around that is.)
pre:
.\Replace-FileByName.ps1 -SearchFileName oldfile.bin -ReplacementFileName d:\newfile.bin -Confirm
This isn't thoroughly tested, I'm sure there's plenty of room for improvement.

This one works great.

Thanks again to both of you!

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply