简体   繁体   中英

Rename EXE's from multiple directories w/ batch file

Can someone help me make my batch file more efficient?

Right now I have one giant script with all of the exe names individually spelled out. As the environment grows, this will become harder to manage. What I'm looking for is something like this:

Folder structure:

- Folder 1: Contains NewExe.exe (new version of my executable) and ClientName.txt files which contains the names of all my clients
 - Folder 2: Some clients reside here
 - Folder 3: Some clients reside here
 - Folder 4: Rest of the clients reside here

Process:

  1. Check folders 2, 3, and 4 for ClientName.exe found in ClientName.txt
  2. If ClientName.exe is found, delete ClientName.exe
  3. Copy NewExe.exe to location of former ClientName.exe
  4. Rename new NewExe.exe to ClientName.exe that was being used in Step 1

Basically, what I am trying to do, is upgrade the EXE used by my clients across the board with one batch file as opposed to spelling everything out or having multiple batch files due to the multiple directories.

This is probably very easy.

Here is my current batch file (I copy and paste it, changing the EXE name each time):

DEL "ClientImport01\ClientName.exe"
COPY "FileServer\NewExe.exe" "ClientImport01\ClientName.exe"

The following is a powershell script that I wrote. Please give it a go and let me know if it works for you. I'm sure this is doable with batch, but it's probably a pain.

cd C:\Temp\BatchTest #Go to your work folder

$filePostfix = '_ProductName.exe';    #Your file postfix like '_ProductName.exe'
$sourceFile = 'ProductName.exe'; #Your source file that replaces all existing client apps like 'ProductName.exe'

$clients = get-content ClientName.txt; #Get all the names from your text file
Write-Host 'Clients found in client file:' -foregroundcolor cyan;
Write-Host $clients;
Write-Host

#Loop through all the names, lookup the location and overwrite the file
$clients | ForEach-Object  {
    $fullName = $_ + $filePostfix;
    $location = Get-ChildItem -Path . -Recurse -Filter $fullName;
    Write-Host 'Client app found: ' $location.FullName -foregroundcolor yellow;
    Copy $sourceFile $location.FullName -Force
}

PS You could make this a lot shorter, but I guess like this it's more readable.

Using @JamesBlond 's help, I created a powershell script with three separate functions, and called each function. The code is as follows:

function CopyEXE {

    cd C:\Temp\BatchTest #Go to your work folder

    $destination = @("C:\Test\")    #The locations to check

    $filePostfix = '_ProductName.exe';    #Your file postfix like '_ProductName.exe'
    $sourceFile = 'ProductName.exe';      #Your source file that replaces all existing client apps like 'ProductName.exe'

    $clients = get-content ClientName.txt; #Get all the names from your text file
    Write-Host ""
    Write-Host "Checking Server"
    Write-Host ""
    Write-Host 'Clients found in client file:' -foregroundcolor cyan;
    Write-Host $clients;
    Write-Host ""

    #Loop through all the names, lookup the location and overwrite the file

    $clients | ForEach-Object  {
        $fullName = $_ + $filePostfix;
        $location = Get-ChildItem -Path $destination -Recurse -Filter $fullName;
        Write-Host 'Client app found: ' $location.FullName -foregroundcolor yellow;
        Copy $sourceFile $location.FullName -Force
        }
    }

CopyExe

So I have the following functions:

  • CopyEXE
  • CopyEXE2
  • CopyEXE3

...and all three have a different $destination variable ($destination, $destination2, $destination3)...

The only issue is if a file does not exist in the destination, it gives the 'can't write over itself message', but that's just a minor nuisance more than anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM