简体   繁体   English

从具有批处理文件的多个目录重命名EXE

[英]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. 现在,我有一个巨型脚本,其中所有exe名称都单独列出。 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 检查ClientName.txt中的ClientName.exe文件夹2、3和4
  2. If ClientName.exe is found, delete ClientName.exe 如果找到ClientName.exe,请删除ClientName.exe
  3. Copy NewExe.exe to location of former ClientName.exe 将NewExe.exe复制到以前的ClientName.exe的位置
  4. Rename new NewExe.exe to ClientName.exe that was being used in Step 1 将新的NewExe.exe重命名为在步骤1中使用的ClientName.exe

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. 基本上,我想做的是使用一个批处理文件全面升级我的客户使用的EXE,而不是拼写所有内容或由于目录而拥有多个批处理文件。

This is probably very easy. 这可能很容易。

Here is my current batch file (I copy and paste it, changing the EXE name each time): 这是我当前的批处理文件(我将其复制并粘贴,每次更改EXE名称):

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

The following is a powershell script that I wrote. 以下是我编写的powershell脚本。 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. 在@JamesBlond的帮助下,我创建了一个具有三个独立功能的Powershell脚本,并调用了每个功能。 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 复制EXE
  • CopyEXE2 复制EXE2
  • CopyEXE3 复制EXE3

...and all three have a different $destination variable ($destination, $destination2, $destination3)... ...而这三个变量都有不同的$ destination变量($ 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. 唯一的问题是,如果目标中不存在文件,它会显示“无法覆盖自身的消息”,但这仅是一个小小的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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