Windows 重命名EXE';来自多个带有批处理文件的目录的

Windows 重命名EXE';来自多个带有批处理文件的目录的,windows,powershell,batch-file,cmd,Windows,Powershell,Batch File,Cmd,有人能帮我提高批处理文件的效率吗 现在我有一个巨大的脚本,所有的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 h

有人能帮我提高批处理文件的效率吗

现在我有一个巨大的脚本,所有的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
过程:

- 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
  • 检查文件夹2、3和4中是否存在ClientName.txt中的ClientName.exe
  • 如果找到ClientName.exe,请删除ClientName.exe
  • 将NewExe.exe复制到以前ClientName.exe的位置
  • 将新的NewExe.exe重命名为步骤1中使用的ClientName.exe

  • 基本上,我正在尝试做的是用一个批处理文件升级我的客户端使用的EXE,而不是因为有多个目录而把所有内容都拼出来或有多个批处理文件

    这可能很容易

    这是我当前的批处理文件(我复制并粘贴它,每次都更改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
    }
    

    PS您可以将其缩短很多,但我想这样更可读。

    使用@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
    
    因此,我有以下功能:

    • CopyEXE
    • 副本2
    • 副本3
    …这三个变量都有不同的$destination变量($destination、$destination2、$destination3)


    唯一的问题是,如果目标中不存在文件,它会给出“无法自行写入”消息,但这只是一个小麻烦。

    您考虑过powershell吗?不是真的……我使用PS比使用batch更原始……但我已经使用过它。我只是不太擅长循环等等。自从我在学校上任何类型的编程课以来,已经有十多年了@Jamesblond为什么要先删除该文件,如果该文件已经存在,为什么不覆盖它?您应该能够使用/Y标志。此外:当您更新exe时,是否存在不应更新的现有客户端exe,或者您正在更新所有这些exe?我想我可以直接覆盖它们。我更喜欢S&G的完全重写,我一直都是这样做的……我正在替换匹配的3个目录中的所有EXE。它们通常命名为ClientName\u ProductName.exe@JamesBlondHey@JamesBlond,我已尝试该脚本并替换了必要的值,但出现以下错误:找到客户端应用程序:复制项目:无法将项目C:\Users\user\Desktop\TFProAdmins\Live\TFProAdmin.exe复制到自身。在C:\Users\user\Desktop\TFProAdmins\Live\TFProCopy.ps1:25 char:9+复制时,消息显示“无法复制项目…”。。。那么你改变了什么价值观,改变了什么?您的ClientName.txt文件中是否有空行?应用程序名称在客户端名称和应用程序名称之间是否真的有一个uIn?对不起,我在ClientName.txt文件中的每个客户端名称的末尾都有一个Tproadmin.exe。我去掉了,效果很好。不过有一件事,每个包含客户机文件的文件夹都位于3个不同的服务器上……我认为您的脚本除了工作目录外,不会在任何地方看到。你知道我可以让它检查3个不同网络位置的名称/文件的方法吗@JamesBlond我会在powershell脚本中有三个“函数”,每个都有不同的工作目录吗?嗨@JamesBlond,很抱歉打扰你,有什么建议吗?我曾尝试将您的函数封装在“foreach$dir in$destination”循环中,但它似乎工作得不太好。给了我一个原始问题,即即使我写了一个write host$dir,它也会正确地向我显示目录,但仍尝试在其自身上写入文件。