PowerShell changes the contents of a text file on Get-Content
or the variable on Set-Content
.
This is a PowerShell script for compiling a bunch of files and formatting them to a single text file, this part checks if the new file is equal to the old one, but according to the comparison, it never is. I've tried messing with stuff like making the new var be a string array as get-content is.
$files = Get-ChildItem -path "./dir" | Where-Object -FilterScript {$_.Name -match '[A-J]'}
$var = @()
foreach ($file in $files){
$filePath = "dir\" + $file;
$fileContent = Get-Content $filepath
$var += $fileContent
}
$firstLine = '"this" "issen" "da" "first"'
$var1 = '"abc" "def" "12" "6"'
$var2 = '"456" "def" "12" "4"'
$newContent = $firstLine,$var,$var1,$var2
$oldContent = Get-Content './File.txt'
if (-not (Compare-Object $oldContent $newContent)) {
Write-Host Nothing new.
} else {
Write-Host Something new
Set-Content './File.txt' -value $newContent
}
Set-Content './File.txt' -value $newContent
pause
When comparing $newContent
to $oldContent
which is just $newContent
stored and retrieved from a text file they are not identical. They should be, right?
The text files that are gathered from ./dir are like this: a00.txt
a12 bc23 abc
b00.txt
a12 bc23 abg