我有一个包含大量安装文件的文件夹。我想采取其中的一些(通过给定模式找到)并将它们镜像到闪存驱动器。
来源:
D:\ccleaner124.exe
D:\dfc221.exe
D:\abc.exe
目标
H:\ccleaner123.exe
H:\dfc221.exe
模式:(直接存储在脚本或某些.txt文件中)
D:\ccleaner*.exe
D:\dfc*.exe
结果:
来源:不变
目的地:
H:\ccleaner124.exe // deleted 123 as it had lower version number in pattern [a-zA-Z_-]*([0-9]*) and copied 124 instead
H:\dfc221.exe // current, so kept the same as it was (no copy)
我查找了Copy-Item函数属性,但是我还没有找到类似“mirror”参数的东西。甚至可以用Power Shell做到这一点吗?
答案 0 :(得分:0)
Bellow工作脚本(我之前写过一些simmliar)。将它放在copy.ps1中,修改sourceDir,destDir和模式数组
function removeName($txt)
{
$inputPattern = "^[a-z]+"
$txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $inputPattern, "");
$inputPattern = "[.][a-z]+"
$txt = [System.Text.RegularExpressions.Regex]::Replace($txt, $inputPattern, "");
return [int]$txt;
}
$pattern = @('a*.txt', 'b*.txt')
$sourceDir = 'C:\source'
$destDir = 'C:\dest'
$pattern | foreach{
$p = $_;
$s = Get-ChildItem -Filter $p -Path $sourceDir;
$sName = removeName $s.Name
$d = Get-ChildItem -Filter $p -Path $destDir;
$dName = removeName $d.Name
if($sName -gt $dName)
{
Write-Host $s.Name
Write-Host $sName
rm -Force $d.FullName
Copy-item $s.FullName $destDir
}
}