比较PowerShell中的文件夹
我使用它递归地比较PowerShell中的两个文件夹: 使用这些行:比较PowerShell中的文件夹,powershell,Powershell,我使用它递归地比较PowerShell中的两个文件夹: 使用这些行: $fso = Get-ChildItem -Recurse -path C:\test\A $fsoBU = Get-ChildItem -Recurse -path C:\test\B Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU 现在我在文件夹B中有一些应该忽略的文件夹(例如.git)。根据以下答案,我尝试过: $fso = Get-Chil
$fso = Get-ChildItem -Recurse -path C:\test\A
$fsoBU = Get-ChildItem -Recurse -path C:\test\B
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
现在我在文件夹B中有一些应该忽略的文件夹(例如.git
)。根据以下答案,我尝试过:
$fso = Get-ChildItem -Recurse -path C:\test\A
$fsoBU = Get-ChildItem -Recurse -path C:\test\B -Exclude @( "C:\test\B\.git" )
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
实际上,.git文件夹中的所有文件都会被跳过,但其他所有文件都会以不同的方式弹出:
我使用Beyond compare双重检查了文件夹比较,没有发现任何差异。这应该可以工作(如果您提前知道要排除的内容):
$fsoBU=Get ChildItem-Recurse-path C:\test\B-Exclude@(“.nuget”、“bin”、“obj”)
您可以在同一命令中使用-Include
和-Exclude
参数,它们接受字符串数组,因此如何构建它取决于您自己,以上只是一个简单的示例(两者都支持通配符)
也考虑使用<代码> -滤镜<代码>,它是一个字符串,但支持通配符等,但是它更好,因为在对象被检索之前应用了过滤器,而<>代码>包含和
-Exclude
参数不仅会将命令的输出更改为引用文件,还会更改为引用其完整路径。至少可以说,这是令人沮丧的。我唯一的建议是:A)对文件系统提供程序运行Get ChildItem时,尽可能使用-Filter
参数,B)在通过Where
语句运行结果后执行所有其他筛选。使用-Include
和-Exclude
从文件系统提供程序中提取所有结果,然后过滤输出,因此PowerShell也在做同样的工作,但使用Where
语句更灵活、更可靠。试试这个:
$fso = Get-ChildItem -Recurse -path C:\test\A
$fsoBU = Get-ChildItem -Recurse -path C:\test\B | Where{$_.FullName -notlike "C:\test\B\.git*"}
Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU
我敢打赌,这一变化将产生你想要的结果
更多详细信息:以上回答了您的问题,但以下是我为找出答案所做的
已创建C:\Temp\Test。在其中放置了File1.csv、File2.csv和File3.csv。复制文件夹,并将其粘贴到创建“C:\Temp\Test-Copy”的所有三个文件中。然后我删除了C:\Temp\Test-Copy\File2.csv。我运行了以下命令:
$dir1 = gci c:\temp\test -recurse
$dir2 = gci 'c:\temp\test - copy' -recurse
diff $dir1 -dif $dir2
我得到的预期结果是,$dir1
有File2.csv,而$dir2
没有。好的,现在要过滤掉一些东西
$dir1 = gci c:\temp\test -recurse -exclude "file1.csv"
diff $dir1 -dif $dir2
什么是什么??它告诉我:
InputObject SideIndicator
----------- -------------
File1.csv =>
File3.csv =>
C:\temp\test\File2.csv <=
C:\temp\test\File3.csv <=
是的,所以ImmediateBaseObject列出了我们使用-Exclude
的结果的完整路径,但没有列出其他结果的完整路径。对我来说,为了应用-Include
或-Exclude
,PowerShell必须引用对象的完整路径,但如果不使用该路径,它只引用对象本身。如何避开这个问题?使用Where
语句在收集结果后过滤结果
这还不能回答为什么?!?,但它确实解释了我是如何来到这里的,希望能在将来帮助你解决类似的问题。有时,更重要的是要弄清楚如何处理你所拥有的,然后弄清楚它为什么会这样工作 应该有用。。。但事实并非如此。如果使用
-Exclude
参数,则它收集的FileInfo对象由完整路径引用,而不是由文件名引用。执行一个$fsoBU[0].PSObject
并查看列出的ImmediateBaseObject
。现在删除-Exclude
,然后再次执行此操作。如果不使用exclude,它只列出文件名,而使用exclude,它会显示完整路径。这很新颖,但如果使用$fso=Get ChildItem-Recurse-path C:\test\a-exclude@(“”
)来应用空筛选器,它似乎会返回相同的文件名和完整路径,因此可以用于比较目的吗?这对你有用吗?
PS:\Temp> $dir1[0].psobject
BaseObject : C:\temp\test\File2.csv
Members : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test\File2.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test, System.String
PSChildName=File2.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Properties : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test\File2.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\test, System.String
PSChildName=File2.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Methods : {string get_Name(), long get_Length(), string get_DirectoryName(), System.IO.DirectoryInfo get_Directory()...}
ImmediateBaseObject : C:\temp\test\File2.csv
TypeNames : {System.IO.FileInfo, System.IO.FileSystemInfo, System.MarshalByRefObject, System.Object}
PS C:\Temp> $dir2[0].psobject
BaseObject : File1.csv
Members : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy\File1.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy, System.String
PSChildName=File1.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Properties : {System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy\File1.csv, System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\temp\Test - Copy, System.String
PSChildName=File1.csv, System.Management.Automation.PSDriveInfo PSDrive=C...}
Methods : {string get_Name(), long get_Length(), string get_DirectoryName(), System.IO.DirectoryInfo get_Directory()...}
ImmediateBaseObject : File1.csv
TypeNames : {System.IO.FileInfo, System.IO.FileSystemInfo, System.MarshalByRefObject, System.Object}