PowerShell-目录-并排比较

时间:2019-09-20 17:06:01

标签: powershell powershell-2.0

我需要在Power Shell中并排比较两个目录结构,但是我无法做到这一点。 寻找这样的输出

Filename   Dir1     Dir2      File exists in Dir1 File exists in Dir2
ABC.txt    c:\data  d:\data   True                False  
ABD.txt    c:\data  d:\data   False               False
ADC.txt    c:\data  d:\data   True                True

$SourceFiles = Get-ChildItem 'c:\data' -File 
$destFiles = Get-ChildItem 'd:\Data' -File 
Compare-Object $sourceFiles $destFiles -IncludeEqual -ExcludeDifferent -Property Name| ForEach-Object { 
   [PSCustomObject]@{ 
   Name = $_.Name 
   Path1 = ($SourceFiles | Where-Object Name -eq $_.Name).Directory 
   Path2 = ($destFiles | Where-Object Name -eq $_.Name).Directory } 
}

1 个答案:

答案 0 :(得分:1)

尝试一下:

$DirSourceName='c:\temp'
$DirDestName='c:\temp2'

$SourceFiles = Get-ChildItem $DirSourceName -File 
$destFiles   = Get-ChildItem $DirDestName -File 

Compare-Object $sourceFiles $destFiles   -Property Name -IncludeEqual | %{
    [pscustomobject]@{
        FileName=$_.Name
        Dir1=$DirSourceName
        Dir2=$DirDestName
        "File exists in Dir1"=($_.SideIndicator -eq "==" -or $_.SideIndicator -eq "<=")
        "File exists in Dir2"=($_.SideIndicator -eq "==" -or $_.SideIndicator -eq "=>")
    }

}