0

我有一个文件夹,里面有很多安装文件。我想取其中一些(通过给定模式找到)并将它们镜像到闪存驱动器。

资源:

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 做到这一点吗?

4

1 回答 1

0

贝娄工作脚本(我前一段时间写了一些类似的东西)。放到copy.ps1中,修改sourceDir、destDir、pattern数组

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
    }
}
于 2013-04-18T06:51:58.153 回答