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
add comment

2 Answers

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
add comment

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. –  BigDave 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? –  BigDave 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
show 2 more comments

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.