Compare files based on LastWriteTime if basename matches

I have two sets of files, one folder contains one filetype and another folder contains the other filetype.

 

Folder 1 has .filetype1
Folder 2 has .filetype2

Every file in folder1 has a matching partner in folder2. The common thing is .basename. To get files where basename matches i'm just doing:

$dir1 | where {$_.BaseName -in $dir2.BaseName}

 

this however is not possible to do when comparing .LastWriteTime as that function will match $_.LastWriteTime to any file in $dir2.LastWriteTime

Pseudo:

If $_.Basename -eq $dir2.basename find files where .LastWriteTime differs  <-- help is wanted

Parents
No Data
Reply
  • So here is a way you can do that, it works by iterating through all items in the first folder and comparing those to the items in the second folder. The statement that does the comparison in the following:

    if (($_.BaseName -eq $File.BaseName) -and ($_.LastWriteTime -ne $File.LastWriteTime))

    Here is the full code:

    Get-ChildItem -Path C:\Testing\Test1 | ForEach-Object -Begin {
    $SecondFolder = Get-ChildItem -Path C:\Testing\Test2
    } -Process {
    foreach ($File in $SecondFolder) {
    if (($_.BaseName -eq $File.BaseName) -and ($_.LastWriteTime -ne $File.LastWriteTime)) {
    $_
    }
    }
    }

    Let me know if this does what you are looking for.

Children