Powershell - 比较文件夹内容

时间:2013-01-14 14:04:35

标签: powershell comparison

我有以下代码比较两个文件夹并输出与每个文件夹不同的文件,有没有办法让它输出子文件夹中文件的完整文件/文件夹路径?

干杯

 # Create varaibles to store folder paths - pass to Strings... 
 param([string]$argFolderA,[string]$argFolderB)

 # Set variables for folder
 $FolderA = Get-ChildItem -Recurse -path $argFolderA
 $FolderB = Get-ChildItem -Recurse -path $argFolderB

 # Compare the contents and output the files different within each folder. 
 Compare-Object -ReferenceObject $FolderA -DifferenceObject $FolderB >> C:\CmpOut.txt

3 个答案:

答案 0 :(得分:5)

使用Comapre-Object的Passthru标志,如下所示:

Compare-Object -ReferenceObject $FolderA -DifferenceObject $FolderB -Passthru | % {$_.FullName}  >> C:\CmpOut.txt

答案 1 :(得分:3)

试试这个:

Compare-Object -ReferenceObject $FolderA -DifferenceObject $FolderB  | 
select @{n="InputObject";e={ 
     if ($_.inputobject -is [system.io.directoryinfo]) 
     {([system.io.DirectoryInfo]$_.InputObject).fullname}
      else 
      { ([system.io.FileInfo]$_.InputObject).fullname}}},SideIndicator 

答案 2 :(得分:1)

通过ForEach {$_.InputObject.FullName}管道。

像这样:

Compare-Object -ReferenceObject $FolderA -DifferenceObject $FolderB | ForEach {$_.InputObject.FullName}