powershellfilecomparegetdate

Comparing original file and last two files using PowerShell


I have another script that outputs data into a csv file every 4 hours and creates the files as vdsinfo_$(Get-Date -Format "yyMMdd_hh-mm-ss").csv.

I want to be able to compare the original file created and also the last two files created against each other to see if there has changes within the file.

At the moment, this is what I have for the script but it isn't quite there yet and would really appreciate some help to get this producing an output written to a Changes.csv file.

I want to compare files that file names are always changing so the script needs to be using variables where possible and only way I can see this working is by the get-date addhours-4.

$File = 'E:\TEMP\Changes.csv'
# Get File item
$file1 = Get-ChildItem -Path E:\TEMP\reports\vdsinfo_original.csv

# Get the specific date
$date1 = (Get-Date).AddHours(-8)

# Compare file modified date with specific date
$file2 = $file1.LastWriteTime -gt $date1

Compare-Object -ReferenceObject $file1  -DifferenceObject $file2  -CaseSensitive | Format-table InputObject, SideIndicator -Autosize | out-file $file -Width 200
Else {
    Write-Host "File missing " -ForegroundColor White -BackgroundColor Red
}

Any help would really help me, thanks in advance

With this current script it only does comparison against two files and I'd like to compare against the original and the last two files within the folder location.


Solution

  • I want to be able to compare the original file created and also the last two files created against each other to see if there has changes within the file.

    This is a bit unclear, but in the following I'm going to assume you want to generate 2 sets of changes:

    1. The full set of all changes between the original file and the latest version
    2. The most recent set of changes - between the next-to-last and the latest version

    only way I can see this working is by the get-date addhours-4.

    That's not necessary at all. The file name contains a sortable date/timestamp, so sort the files by name, then you can simply jump to the previous file in your sorted list and get the data from 4 hours prior:

    # define output destinations
    $outFilePaths = @{
      All = 'E:\TEMP\AllChanges.csv'
      Latest = 'E:\TEMP\LatestChanges.csv'
    }
    
    # find the original file
    $originalFile = Get-Item -Path E:\TEMP\reports\vdsinfo_original.csv
    
    # find all the subsequent versions of the file, make sure 
    # they're sorted by name, then store them in an array
    $files = @(Get-ChildItem -Path E:\TEMP\reports -Filter vdsinfo_[0-9]*.csv |Sort-Object Name)
    
    # make sure we've located at least one subsequent version - otherwise there's nothing to compare against!
    if ($files.Count -gt 0) {
        # let's start by comparing original with the latest version
        # 
        # $files[-1] is going to evaluate to the _last_ item in the $files array
        $allChanges = Compare-Object -ReferenceObject ($originalFile |Get-Content) -DifferenceObject ($files[-1] |Get-Content)
    
        # output to a file
        $allChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $filePaths['All'] -Width 200
    
        # repeat comparison between the two latest versions
        $latestChanges = Compare-Object -ReferenceObject ($files[-2] |Get-Content) -DifferenceObject ($files[-1] |Get-Content)
    
        # output to a file
        $latestChanges |Format-table InputObject, SideIndicator -Autosize |Out-File $filePaths['Latest'] -Width 200
    }
    else {
        Write-Host "Updated files are missing" -ForegroundColor White -BackgroundColor Red
    }