Sample of file names in our naming convention using PowerShell:
SYSTEM_20201019-01_PRE folder
- SYSTEM_20201118_file1.txt
- SYSTEM_20201118_file2.csv
- SYSTEM_20201118_file3_and_file4.txt
SYSTEM_20201019-01_POST folder
- SYSTEM_20201119_file1.txt
- SYSTEM_20201119_file2.csv
- SYSTEM_20201119_file3_and_file4.txt
We use this convention and it worked well as simply "SYSTEM_file1.txt" before we added the date. If we include the date in there, our script will only compare them if the date matches (which makes sense). This works mostly because the filenames are typically the same in the before and after. However, sometimes the dates will change and we simply have to manually rename all the files in the before, and then it works properly. We want to alleviate this manual work for others.
Is there a way I can take the PRE and POST and redefine them parsing out the first XX characters to only compare based on the name after the second _
character? The filenames, filename lengths, and extensions vary so I need to start from the beginning and not the end.
Here's the full code:
if($changeStatus -eq "POST"){
Write-Host "`n`n*********************************************"
Write-Host "`nComparing all PRE/POST files for differences."
$folderPath = $changeID + "_" + $env:COMPUTERNAME + "_"
$postFolderPath = $folderPath + "POST"
$preFolderPath = $folderPath + "PRE"
$comparisonFile = "$postFolderPath\Output_Comparison.txt"
if(!(Test-Path $preFolderPath)){
Write-Host "Missing PRE folder with the associated ChangeID. Unable to perform comparison."
Write-Host "Press any key to exit......"
Read-Host
exit
}
else{
$fileName = Get-ChildItem -Path $postFolderPath | Select-Object -ExpandProperty Name
ForEach ($file in $fileName){
$preFile = Get-Content ($preFolderPath + "\" + $file)
$postFile = Get-Content ($postFolderPath + "\" + $file)
$comparisonOutput = Compare-Object $preFile $postFile | Where-Object {($_.SideIndicator -eq "=>") -and (($_.InputObject -replace '"',"") -ne $dateRan)} | Select-Object -ExpandProperty InputObject
if($comparisonOutput -ne $null){
if(Test-Path $comparisonFile){
"`r`n`r`nDifference in file $file" | Add-Content -Path $comparisonFile
$comparisonOutput | Add-Content -Path $comparisonFile
}
else{
"The following are the diffences found in POST files:" | Out-File -FilePath $comparisonFile
"`r`nDifference in file $file" | Add-Content -Path $comparisonFile
$comparisonOutput | Add-Content -Path $comparisonFile
}
}
}
}
}
$before
,$after
, and$SYSTEM.Length
? ///// are you limited to ps3? if so, what operating system are you running?