I have a PowerShell script that compares yesterday's and today's file to provide the changes that can be further loaded into an Oracle table for reporting needs. My trouble is that over the weekends when there is no change in the source system, there are no new rows in today's file compared to yesterday's (the file1 and file2 are identical). Compare-Object
still produces an output file that just has a new line character. I searched this forum for anyone who might have had a similar issue and below is what I have tried so far.
Initial code:
Compare-Object -ReferenceObject $(Get-Content $filenew) -DifferenceObject $(Get-Content $fileold) |
Where-Object SideIndicator -eq '<=' |
%{$_.Inputobject + $_.SideIndicator} |
ft -Auto |
Out-File $file_diff -Width 5000
I tried adding below to my code and it still doesn't help
$filen = Get-Item "D:\DevelopTest\DEV\Test\File2.txt"
$fileo = Get-Item "D:\DevelopTest\DEV\Test\File1.txt"
Compare-Object -ReferenceObject $(Get-Content $filenew) -DifferenceObject $(Get-Content $fileold) |
Where-Object SideIndicator -eq '<=' |
%{$_.Inputobject + $_.SideIndicator} |
ft -Auto |
Out-File $file_diff -Width 5000
(gc "D:\DevelopTest\DEV\Test\File.txt") |
? {$_.Trim() -ne "" } |
Set-Content $file_diff
What can I add to identify the file has no data (just blank/new line characters) so I can prevent loading null rows to the table. Any help is appreciated, thanks in advance.
It is not Compare-Object
which produces empty/null lines.
$filenew = Get-Item "D:\DevelopTest\DEV\Test\File2.txt"
$fileold = Get-Item "D:\DevelopTest\DEV\Test\File1.txt"
$Differences = Compare-Object -Ref (Get-Content $filenew) -Diff (Get-Content $fileold)
if ($Differences.Count){
$Differences | ForEach-Object{
$_.Inputobject + $_.SideIndicator
} | Out-File $file_diff -Width 5000
}