繁体   English   中英

Powershell比较对象返回错误结果

[英]Powershell Compare-Objects returns wrong results

我正在尝试使用以下命令比较2个文件夹的内容:

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

这是我应该得到的结果:

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

这是我实际上得到的(我已经注意到两个文件夹中都存在对象,应该将它们从比较中排除):

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 <=  

为什么Powershell将两个文件夹中都存在的对象标记为不存在? 好像我正在将-IncludeEqual与Compare-Object一起使用(不是),但是不是==侧指示器,而是得到了<=和=>。

您的比较应该是

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)

这是因为,正如@TessellatingHeckler指出的那样,两条路径之间的驱动器号(限定符)总是不同的,并且您想忽略该部分。

暂无
暂无

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

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