简体   繁体   English

比较目录和子文件夹

[英]Compare directory with sub-folders

I have been have a tough time with this but I am trying to do a compare between two servers and have them compare all of the files from one server to the other. 我经历了一段艰难的时光,但我试图在两台服务器之间进行比较,并让它们将一台服务器与另一台服务器的所有文件进行比较。 Even inside the subfolders. 即使在子文件夹中。 So far I've gotten it to compare the folder names and files but can't get it to go inside the folders and compare the contents. 到目前为止,我已经得到它来比较文件夹名称和文件,但是无法让它进入文件夹并比较内容。 Here's a few things I've tried. 这是我尝试过的几件事。

$A = Get-ChildItem -Recurse -path $Path
$B = Get-ChildItem -Recurse -path $PAth1
Compare-Object -ReferenceObject $A -DifferenceObject $B -PassThru

This is what I started with and it works the best but still doesn't go inside the sub-folders. 这是我开始时的工作,并且效果最好,但仍然不在子文件夹中。 I also tried to use foreach statements with Arrays to store the content in an array and compare the arrays but this doesn't seem to be working at all. 我还尝试将foreach语句与Arrays一起使用以将内容存储在数组中并比较数组,但这似乎根本不起作用。

$FileDirectoryA = "Path"
$FileDirectoryC = "path"
$A = foreach($folderA in Get-ChildItem $fileDirectoryA)
{
    $FolderA
}
$B = foreach($FileA in Get-ChildItem $ArrayA)
{
    $FileA
}
$C = foreach($folderC in Get-ChildItem $fileDirectoryC)
{
    $FolderC
}
$D = foreach($FileC in Get-ChildItem $ArrayC)
{
    $FileC
}

Compare-Object $B $D

When I try and just do a 当我尝试做一个

Compare-Object $Path $Path2 

It errors on all of the folders saying permission denied which doesn't make much sense to me. 在所有文件夹上均显示错误,表示拒绝权限,这对我来说没有太大意义。

Thanks, Andrew 谢谢,安德鲁

It should work if you specify the property to compare : 如果您指定要比较的属性,它将起作用:

$a = ls c:\temp\ -rec
$b = icm -computername $servername -scriptblock{ls c:\temp\ -rec}
compare $a $b -Property fullname

A more reliable way might be to use robocopy robocopy.exe \\\\serverA\\Folder1 \\\\serverB\\Folder2 /e /l /log:c:\\temp\\dif.txt 一种更可靠的方法可能是使用robocopy robocopy.exe \\\\serverA\\Folder1 \\\\serverB\\Folder2 /e /l /log:c:\\temp\\dif.txt

I think this powershell script https://github.com/oflisback/dirdiff addresses your problem, it scans two directories and reports differences: 我认为这个powershell脚本https://github.com/oflisback/dirdiff解决了您的问题,它扫描了两个目录并报告了差异:

 PS C:\\> dirdiff.ps1 C:\\dir1 C:\\dir2 One identical file: .\\identical.txt One file only present in C:\\dir1: .\\newdir1.txt 2 files only present in C:\\dir2: .\\newdir2.txt .\\newdir\\newfilesdir2.txt One file present in both directories and has different content: .\\modified.bin 

The script is based on this gist so credit to cchamberlein . 该脚本基于此要旨,因此值得cchamberlein使用

This should give you a reasonable comparison I think. 我认为这应该给您一个合理的比较。 You can use regex on the return values to rebuild the original filepaths if you so desire. 如果需要,可以在返回值上使用正则表达式来重建原始文件路径。

$Dir1="C:\Test"
$Dir2="C:\Test2"
$A=(Get-ChildItem $Dir1 -Recurse).VersionInfo.Filename -replace [regex]::Escape($Dir1),""
$B=(Get-ChildItem $Dir2 -Recurse).VersionInfo.Filename -replace [regex]::Escape($Dir2),""
Compare-Object $A $B

This reuslts in an output like... 这会在输出中重新显示,例如...

InputObject                                                        SideIndicator                                                     
-----------                                                        -------------                                                     
\New folder\temp.txt                                               =>                                                                
\temp.txt                                                          <=                                                                
\New folder\New folder\temp.txt                                    <=           

Update 更新

I think should do everything you are looking for. 我认为应该做您想要的一切。 You'll need to change the $Dir1 and $Dir2 values. 您需要更改$Dir1$Dir2值。 It's likely over-complicated but it does appear to work. 它可能过于复杂,但确实起作用。

$Dir1="C:\Test"
$Dir2="C:\Test2"
$Items1=Get-ChildItem $Dir1 -Recurse
$Items2=Get-ChildItem $Dir2 -Recurse
$A=$Items1.VersionInfo.Filename -replace [regex]::Escape($Dir1),""
$B=$Items2.VersionInfo.Filename -replace [regex]::Escape($Dir2),""
$Exist_Diff=Compare-Object $A $B

$Exist_Diff | %{
    $Current_Item=$_.InputObject
    if($_.SideIdicator -eq "<="){
        Write-Host "No file equivilent to $Dir1$Current_Item found in $Dir2"
        }else{
        Write-Host "No file equivilent to $Dir2$Current_Item found in $Dir1"
        }
    }

$Items1 | %{
    if ($Exist_Diff.InputObject -notcontains ($_.VersionInfo.Filename -replace [regex]::Escape($Dir1),""))
        {
        $MatchingFile=[string]$_.VersionInfo.Filename -replace [regex]::Escape($Dir1),$Dir2      
        try{if(Test-Path $MatchingFile)
            {
            if((Get-Item $MatchingFile).length -gt $_.Length)
                {
                Write-host $_.fullname "is larger than" $MatchingFile
                }
            }}catch{}
        }
    }
$Items2 | %{
    if ($Exist_Diff.InputObject -notcontains ($_.VersionInfo.Filename -replace [regex]::Escape($Dir1),""))
        {
        $MatchingFile=[string]$_.VersionInfo.Filename -replace [regex]::Escape($Dir2),$Dir1      
        try{if(Test-Path $MatchingFile)
            {
            if((Get-Item $MatchingFile).length -gt $_.Length)
                {
                Write-host $_.fullname  "is larger than" $MatchingFile
                }
            }}catch{}
        }
    }
Get-ChildItem $Source -Recurse | ForEach-Object{

   $Dest = Join-Path $Destination $_.FullName.Substring($Source.Length)

   $bool = Test-Path $Dest

   if(!$bool){
       #Write of some thing here to indicate difference
   }

}

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

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