Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to find differences in the content of two folder structures using Windows Powershell. I have used the following method to ensure that the file names are the same, but this method does not tell me if the contents of the files are the same:

$firstFolder = Get-ChildItem -Recurse folder1
$secondFolder = Get-ChildItem -Recurse folder2
Compare-Object -ReferenceObject $firstFolder -DifferenceObject $secondFolder

The technique described in this ServerFault question works for diffing a single file, but these folders contain hundreds of files at a variety of depths.

The solution does not necessarily need to tell me what specifically in the files is different - just that they are. I am not interested in differences in metadata such as date, which I already know to be different.

share|improve this question

3 Answers 3

If you want to wrap the compare into a loop I would take the following approach:

$folder1 = "C:\Users\jscott"
$folder2 = "C:\Users\public"

# Get all files under $folder1, filter out directories
$firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }

$firstFolder | ForEach-Object {

    # Check if the file, from $folder1, exists with the same path under $folder2
    If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {

        # Compare the contents of the two files...
        If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {

            # List the paths of the files containing diffs
            $_.FullName
            $_.FullName.Replace($folder1, $folder2)

        }
    }   
}

Note that this will ignore files which do not exist in both $folder1 and $folder2.

share|improve this answer

You just wrap a loop around the correct answer from your linked question that already answered this, and walk the directory tree comparing every file with the same name.

/Edit : If that's actually your question, it's more appropriate for SO, where you seem to be a regular contributor. You're asking a programming question. I understand you're doing it for a sysadmin-type of purpose, in which case, I would tell you to use a purpose-built tool like WinDiff.

share|improve this answer
2  
OK, this site is not appropriate for "Give me the codez" type questions. If you need to get started on learning how to do loops in Powershell, buy a book or find online tutorials; there are many. –  mfinni Aug 19 '13 at 16:49
1  
I think you woke up on the wrong side of the bed. I am not a regular Powershell user. I have demonstrated in my question both a technique that I am currently attempting, and a link to a question that has additional helpful information for my problem. I do not know how to combine the two techniques, which is my problem, and the reason I have asked this question. It is also a question which I have been unable to answer using Google searches. If you aren't going to be helpful, please consider deleting your answer. –  David Smith Aug 19 '13 at 16:53
2  
@BigDave Literally the first result for PowerShell loop through files on Google. c'mon now - a little effort on your part? –  voretaq7 Aug 19 '13 at 17:01
1  
@voretaq7 I think you guys misunderstand me as a PowerShell user. I did that google search, attempted that technique, and did not succeed. I tried to explain where I'm at in the question above. The question you link to works on a single set of files. I have two sets that I need to compare, name-to-name. I'm really not trying to be lazy here. I know how to loop, and I know how to compare. How do I loop, and compare two sets? –  David Smith Aug 19 '13 at 17:13
1  
I think that doing this with PS is a great little project. However, WinMerge (thought it was WinDiff, silly me) is really a great tool if you're going to be doing this very often. It's literally built for the job. Try it, the download is free. It's got decomposers for most file types, and does a great job of highlighting, including options for how you want to handle whitespace. –  mfinni Aug 19 '13 at 17:26

I have taken jscott's answer an expanded it to output the files that are present in one but not the other for those who are insterest in that type of functionality. Please note it also shows progress made since it was hard for me to see that given the huge folders with not very many differences. It looked like the script was hung to me. Here is the powershell code for that:

$folder1 = "C:\Folder1"
$folder2 = "C:\Folder2"

# Get all files under $folder1, filter out directories
$firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }

$failedCount = 0
$i = 0
$totalCount = $firstFolder.Count
$firstFolder | ForEach-Object {
    $i = $i + 1
    Write-Progress -Activity "Searching Files" -status "Searching File  $i of     $totalCount" -percentComplete ($i / $firstFolder.Count * 100)
    # Check if the file, from $folder1, exists with the same path under $folder2
    If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {
        # Compare the contents of the two files...
        If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {
            # List the paths of the files containing diffs
            $fileSuffix = $_.FullName.TrimStart($folder1)
            $failedCount = $failedCount + 1
            Write-Host "$fileSuffix is on each server, but does not match"
        }
    }
    else
    {
        $fileSuffix = $_.FullName.TrimStart($folder1)
        $failedCount = $failedCount + 1
        Write-Host "$fileSuffix is only in folder 1"
    }
}

$secondFolder = Get-ChildItem -Recurse $folder2 | Where-Object { -not $_.PsIsContainer }

$i = 0
$totalCount = $secondFolder.Count
$secondFolder | ForEach-Object {
    $i = $i + 1
    Write-Progress -Activity "Searching for files only on second folder" -status "Searching File  $i of $totalCount" -percentComplete ($i / $secondFolder.Count * 100)
    # Check if the file, from $folder2, exists with the same path under $folder1
    If (!(Test-Path($_.FullName.Replace($folder2, $folder1))))
    {
        $fileSuffix = $_.FullName.TrimStart($folder1)
        $failedCount = $failedCount + 1
        Write-Host "$fileSuffix is only in folder 2"
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.