Author Posts

September 27, 2015 at 10:45 pm

Here I am able to compare 2 web.configs such that spaces are ignored while comparison. But I want to find a way to ignore blank lines too during comparison. How should I do it?
path1-> path of folder containing web.config files to compare with those of path2
path2-> path of folder containing web.config files to compare with those of path1

function Diff_Content{
param($path1,$path2)
$count1=(Get-ChildItem -Path $path1 -Filter *.config).count
$count2=(Get-ChildItem -Path $path2 -Filter *.config).count
if ($count1 -ne $count2)
{
Write-Host("Number of web.config files is different !")
$folder1=Get-ChildItem -Path $path1 -Filter *.config
$folder2=Get-ChildItem -Path $path2 -Filter *.config
Compare-Object -ReferenceObject $folder1 -DifferenceObject $folder2
}
else{
$i=0;
while($count1 -gt 0)
{

$path3=(Get-ChildItem -Path $path1 -Filter *.config)[$i].FullName
$path4=(Get-ChildItem -Path $path2 -Filter *.config)[$i].FullName
$i=$i+1;
$a = (sls '^' -Path $path3) -replace '.:[^:]+:(.+)','$1' -replace " " 
$b = (sls '^' -Path $path4) -replace '.:[^:]+:(.+)','$1' -replace " " 
$file1 = Split-Path $path3 -Leaf

$file2 =Split-Path $path4 -Leaf
$result = switch (diff $b $a ){

	{ $_.sideindicator -eq '=>' } { $file1 + $_.inputobject }
	{ $_.sideindicator -eq '< =' } { $file2 + $_.inputobject }
}
$count1=$count1-1
}
}

$result
}

October 7, 2015 at 12:02 pm

If you're using a regex, /s represents all whitespace, and /S represents all non-whitespace. Could you simply replace all non-whitespace with an empty string ("") to get rid of it?

October 9, 2015 at 4:47 am

Getting it to work with Regex is surprisingly difficult.
I was convinced something like this would work:

[Regex]::Replace((Get-Content $filename -Raw),"^\s*$",'',[System.Text.RegularExpressions.RegexOptions]::Multiline)

But I can't get it to.

Select-String seems a little better at handling it. Change the pattern lines to:

$a = Select-String -Pattern "^\S+$" -path $file1|Select -ExpandProperty Line

And it should work. You won't need all the -Replace gubbins anymore either, the Select statement takes care of that for you.