Powershell 如何使用给定模式(通配符)镜像文件

Powershell 如何使用给定模式(通配符)镜像文件,powershell,mirror,copy-item,Powershell,Mirror,Copy Item,我有一个文件夹,里面有很多安装文件。我想采取其中一些(发现由给定的模式),并镜像到闪存驱动器 来源: D:\ccleaner124.exe D:\dfc221.exe D:\abc.exe H:\ccleaner123.exe H:\dfc221.exe 目的地: D:\ccleaner124.exe D:\dfc221.exe D:\abc.exe H:\ccleaner123.exe H:\dfc221.exe 模式:(直接存储在脚本或某些.txt文件中) 结果: D:\cclean

我有一个文件夹,里面有很多安装文件。我想采取其中一些(发现由给定的模式),并镜像到闪存驱动器

来源:

D:\ccleaner124.exe
D:\dfc221.exe
D:\abc.exe
H:\ccleaner123.exe
H:\dfc221.exe
目的地:

D:\ccleaner124.exe
D:\dfc221.exe
D:\abc.exe
H:\ccleaner123.exe
H:\dfc221.exe
模式:(直接存储在脚本或某些.txt文件中)

结果:

D:\ccleaner124.exe
D:\dfc221.exe
D:\abc.exe
H:\ccleaner123.exe
H:\dfc221.exe
资料来源:未变

目的地:

我查找了复制项函数属性,但在那里没有找到类似“mirror”参数的内容。甚至可以用Power Shell来实现这一点吗?

下面的工作脚本(我以前写过一些简单的东西)。将其放入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
    }
}

复制项目不是机器人复制的替代品。您必须编写自定义检查/筛选器。