有人可以帮助我提高批处理文件效率吗?
现在我有一个巨大的脚本,所有的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
过程:
基本上,我要做的是用一个批处理文件升级我的客户端使用的EXE,而不是拼写所有内容或由于多个目录而有多个批处理文件。
这可能很容易。
这是我当前的批处理文件(我复制并粘贴它,每次更改EXE名称):
DEL "ClientImport01\ClientName.exe"
COPY "FileServer\NewExe.exe" "ClientImport01\ClientName.exe"
答案 0 :(得分:0)
以下是我写的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你可以缩短它,但我想这样更可读。
答案 1 :(得分:0)
使用@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
所以我有以下功能:
...并且所有三个都有一个不同的$目标变量($ destination,$ destination2,$ destination3)......
唯一的问题是如果目的地中不存在文件,它会给出“不能自己写消息”,但这只是一个小麻烦。