简体   繁体   English

Powershell比较对象返回错误结果

[英]Powershell Compare-Objects returns wrong results

I'm trying to compare the contents of 2 folders using this command: 我正在尝试使用以下命令比较2个文件夹的内容:

Compare-Object (Get-ChildItem C:\\compare -recurse) (Get-ChildItem J:\\compare -recurse) -Property FullName

This is the result that I should get: 这是我应该得到的结果:

FullName                       SideIndicator
--------                       -------------         
J:\compare\test1.txt           =>                     
C:\compare\install.msi         <=           
C:\compare\setup.exe           <=                    
C:\compare\subfolder\test3.txt <=     

This is what I actually get (I've noted where objects exist in both folders and should be excluded from the comparison): 这是我实际上得到的(我已经注意到两个文件夹中都存在对象,应该将它们从比较中排除):

FullName                       SideIndicator
--------                       -------------
J:\compare\subfolder           => (exists in both folders)          
J:\compare\doc1.pdf            => (exists in both folders)           
J:\compare\doc2.pdf            => (exists in both folders)           
J:\compare\test1.txt           =>           
J:\compare\subfolder\test2.txt => (exists in both folders)           
C:\compare\subfolder           <= (exists in both folders)           
C:\compare\doc1.pdf            <= (exists in both folders)           
C:\compare\doc2.pdf            <= (exists in both folders)           
C:\compare\install.msi         <=           
C:\compare\setup.exe           <=           
C:\compare\subfolder\test2.txt <= (exists in both folders)           
C:\compare\subfolder\test3.txt <=  

Why is Powershell flagging objects that exist in both folders as not existing in either folder? 为什么Powershell将两个文件夹中都存在的对象标记为不存在? It's as if I'm using -IncludeEqual with Compare-Object (which I'm not), but instead of the == side indicators I'm getting <= and => instead. 好像我正在将-IncludeEqual与Compare-Object一起使用(不是),但是不是==侧指示器,而是得到了<=和=>。

Your comparison should be 您的比较应该是

Compare-Object `
  (Get-ChildItem C:\Compare -Recurse | Select-Object -ExpandProperty FullName | Split-Path -NoQualifier) `
  (Get-ChildItem J:\Compare -Recurse | Select-Object -ExpandProperty FullName | Split-Path -NoQualifier)

This is because, as @TessellatingHeckler noted, the drive letters (qualifiers) are always different between the two paths, and you want to ignore that part. 这是因为,正如@TessellatingHeckler指出的那样,两条路径之间的驱动器号(限定符)总是不同的,并且您想忽略该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM