繁体   English   中英

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

[英]Rename EXE's from multiple directories w/ batch file

有人可以帮我提高批处理文件的效率吗?

现在,我有一个巨型脚本,其中所有exe名称都单独列出。 随着环境的发展,这将变得更加难以管理。 我正在寻找的是这样的:

资料夹结构:

- 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

处理:

  1. 检查ClientName.txt中的ClientName.exe文件夹2、3和4
  2. 如果找到ClientName.exe,请删除ClientName.exe
  3. 将NewExe.exe复制到以前的ClientName.exe的位置
  4. 将新的NewExe.exe重命名为在步骤1中使用的ClientName.exe

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

这可能很容易。

这是我当前的批处理文件(我将其复制并粘贴,每次更改EXE名称):

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

以下是我编写的powershell脚本。 请尝试一下,让我知道它是否适合您。 我确定这可以批量处理,但是可能很痛苦。

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
}

附注:您可以将其缩短一些,但我想像这样更易读。

在@JamesBlond的帮助下,我创建了一个具有三个独立功能的Powershell脚本,并调用了每个功能。 代码如下:

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

因此,我具有以下功能:

  • 复制EXE
  • 复制EXE2
  • 复制EXE3

...而这三个变量都有不同的$ destination变量($ destination,$ destination2,$ destination3)...

唯一的问题是,如果目标中不存在文件,它会显示“无法覆盖自身的消息”,但这仅是一个小小的麻烦。

暂无
暂无

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

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