0

I wrote this code in PowerShell script but for some reason it's not working. The program itself is working but the replacing of multiline is not working - can anyone shed any light as to why?

Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | 
Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "SCD (*.scd)| *.scd"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}

$inputfile = Get-FileName "C:\Users\Mr.Nephilim\Desktop\ABB"
#$inputdata = get-content $inputfile

#$configFiles = Read-Host 'Enter Complete directory to file'
(Get-Content $inputfile) |
Foreach-Object { 
$_ -replace  '(?ms)^\[\t\n\v\f\r ]<Private type="ABB.*?</Private>', ''
   #-replace '<Terminal .+\/>', '' `
   #-replace '<Terminal .*?>\s<\/Terminal>', '' `
   #-replace '<Private type=\"ABB(.|\n)*?<\/Private>', '' `
   #-replace '<ConnectivityNode.*?>(.|\n)*?<\/ConnectivityNode>', ''
   #-replace '<Terminal .+/>', '' `
   #-replace '<Private type="ABB.*?>.*?</Private>', '' `
   #-replace '<Terminal .+>', '' `
   #-replace '<Private type=\"ABB.*?>', '' `
   #-replace '<esld:.*?>', '' `
   #-replace '<esld:.*?\/>', '' `
   #-replace '</esld:.*?>', '' `
   #-replace '</Private.*?>', '' `
   #-replace '</Terminal.*>', '' `
   #-replace '<ConnectivityNode.*?>', '' `
   #-replace '</ConnectivityNode.*?>', '' 

} |

Set-Content $inputfile

(GC $inputfile)|?{$_.Trim(" `t")}|SC $inputfile
halfer
  • 19,824
  • 17
  • 99
  • 186
Inno BoY
  • 5
  • 4
  • Maybe you're better off fixing the "replacing of multiline is not working" problem. What does "not working" mean? What do you mean by multiline? – doctorlove Aug 03 '17 at 12:01
  • `\[\t\n\v\f\r ]` -> `\s`, `.*?` -> `[\s\S]*?`? – Ansgar Wiechers Aug 03 '17 at 12:04
  • 1
    Maybe [this](https://stackoverflow.com/questions/21911293/multiline-regex-in-powershell) can help you resolve the multiline issue? – MatSnow Aug 03 '17 at 12:07
  • https://stackoverflow.com/questions/45440774/powershell-script-for-replacing-text-of-xml-format-multiple-lines check this out doctorlove – Inno BoY Aug 03 '17 at 12:08
  • Ansgar \s is not supported in powershell i think... any way i have tried the first 2 i'll see if [\s\S]*? works... – Inno BoY Aug 03 '17 at 12:09
  • Can you explain exactly what you're trying to do here? Open a dir, select a file and then what? – Bailey Miller Aug 03 '17 at 12:20
  • Open a file of a specific format using gui(from user), then load all its content, find the xml tags and replace them with empty string. To find a xml tag i'm using regex such as etc tags which are self closing and are on a single line are easy to replace but tags which span over multiple lines are just not working. I run the regex written for multiple line on reg testers and they work just fine but when run on powershell they do nothing. – Inno BoY Aug 03 '17 at 12:25
  • For example: i want to replace with nothing i do ... -replace '', '' and it works but... for multiple lines such as i write – Inno BoY Aug 03 '17 at 12:44
  • @InnoBoY `" " -match "\s"` definitely works by the way. – colsw Aug 03 '17 at 13:03
  • Converting this to another language is too broad for a Stack Overflow question, so I have modified it a bit, to ask for why the multiline issue does not work in the code that you have. – halfer Aug 03 '17 at 21:01
  • @InnoBoY I can assure you that `\s` works just fine in PowerShell (plus, you need the content of the file in a single string, as MatSnow pointed out). With that said, the comments to your previous question still stand. You'd get a much better result if you showed the desired output for your sample input. – Ansgar Wiechers Aug 03 '17 at 21:05
  • ConnorLSW : Where would in the above code i'll put that line? Angsar : the output i want is the to find a string and replace it with empty string basically find and delete. – Inno BoY Aug 04 '17 at 14:48

1 Answers1

0

(?s)(.|\s).*? using this way it can be done...

Inno BoY
  • 5
  • 4