我得到了一个脚本,需要比较两个文件夹中的文件,如果文件只存在于目标中,则必须将其删除。为了删除文件,我不知道该如何更改Compare-Item的输出。另外,在delete中,我必须更改输出,使其在_之前包含一个*。
该脚本如下所示:
$LocalPath = "C:\test5\old"
$localdestination = "C:\test5\New"
$SOURCE = Get-ChildItem -File "*.qvd" $localpath | Foreach-Object { $_.Name -replace "^.*(?=_)", "" }
$Destination = Get-ChildItem -File "*.qvd" $localdestination | Foreach-Object { $_.Name -replace "^.*(?=_)", "" }
$compare = Compare-Object -ReferenceObject $Source -DifferenceObject $Destination
foreach($c in $compare)
{
Remove-Item
}
发布于 2019-11-29 15:46:45
如果我没理解错你的要求,你可以使用下面的代码片段:
$LocalPath = "C:\test5\old"
$localdestination = "C:\test5\New"
$SOURCE = Get-ChildItem -Path $localpath -File '*.qvd'
$Destination = Get-ChildItem -Path $localdestination -File '*.qvd'
$comparisonFileList = Compare-Object -ReferenceObject $Source -DifferenceObject $Destination -Property BaseName -PassThru |
Where-Object {$_.SideIndicator -eq '=>'}
foreach ($comparisonFile in $comparisonFileList) {
Remove-Item -Path $comparisonFile.FullName
}
首先,您创建了2个包含“完整”信息的数组-没有替换某些内容的Foreach-Object
。然后比较这两个数组中项的一个特定属性。然后,使用此比较创建的列表对其进行迭代,并使用先前创建的对象的另一个属性通过其FullName删除文件。
发布于 2019-12-03 11:48:57
$LocalPath = "C:\Source\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Maximo7"
$localdestination = "C:\Dest\Qlikview Storage\PrivateData\Gemensamma\Qvd_Raw\Maximo7"
$SOURCE = Get-ChildItem -File "*.qvd" $localpath | Foreach-Object { $_.Name -replace "^.*(?=_)", "" }
$Destination = Get-ChildItem -File "*.qvd" $localdestination | Foreach-Object { $_.Name -replace "^.*(?=_)", "" }
$compare = Compare-Object -ReferenceObject $Source -DifferenceObject $Destination |
Where-Object {$_.SideIndicator -eq '=>'}
$comparison = $compare.InputObject
foreach($comp in $comparison)
{
$comparisonfile = "maximo" + $comp
Remove-Item $localdestination\$comparisonfile
}
这是我最后提出的解决方案,我把你的一些东西带到了Olaf的工作中,谢谢你的投入。
https://stackoverflow.com/questions/59106655
复制