Home > OS >  In PowerShell not able to locate and Replace String, This String is like a path with '%'
In PowerShell not able to locate and Replace String, This String is like a path with '%'

Time:11-03

I am trying to Update Logo in the Htm Files we have two Path need to be replaced

Following one is working fine and getting Replaced.

Raw String

<img src=C:\ProductName\ProductNameClient\webapps\ProductNameClient\assets\img\logo\logo.jpg alt='ProductName Logo'>

Update due to Escape Sequence issue , we are using following String in Powershell to search for Raw string

<img src=C:\\ProductName\\ProductNameClient\\webapps\\ProductNameClient\\assets\\img\\logo\\logo.jpg alt='ProductName Logo'>

Replace String

<img src=../../ProductNameNew/ProductNameNewLogo.png alt='ProductNameNew Logo'>

PowerShell Script working fine for this String

 Get-ChildItem -Path (Get-Item .).FullName -Recurse -File *.htm | ForEach-Object {
 $content = Get-Content -Path $_.FullName
  if($content -match "<img src=C:\\ProductName\\ProductNameClient\\webapps\\ProductNameClient\\assets\\img\\logo\\logo.jpg alt='ProductName Logo'>"){
     $content -replace "<img src=C:\\ProductName\\ProductNameClient\\webapps\\ProductNameClient\\assets\\img\\logo\\logo.jpg alt='ProductName Logo'>","<img src=../../ProductNameNew/ProductNameNewLogo.png alt='ProductNameNew Logo'>" | Set-Content -Path $_.FullName
  }}

I used the same powershell script and Update the String for escape sequence

Not working with this String Raw string

<img src=C:\Program Files (x86)\ProductName\.\ProductNameConfigs\ProductNameOLDlogo.png alt='ProductName Logo'>

Update due Escape Sequence issue

<img src=C:\\Program Files (x86)\\ProductName\\.\\ProductNameConfigs\\ProductNameOLDlogo.png alt='ProductName Logo'>

Replace String

<img src=../../../ProductNameNew/ProductNameNewLogo.png alt='ProductNameNew Logo'>

i used the escape sequence that worked for previous Update but now its not working i am not getting any error to resolve this in powershell, Script will execute, string will remain unchanged and no error in powershell

Get-ChildItem -Path (Get-Item .).FullName -Recurse -File *.htm | ForEach-Object {
 $content = Get-Content -Path $_.FullName
  if($content -match "<img src=C:\\Program Files (x86)\\ProductName\\.\\ProductNameConfigs\\ProductNameOLDlogo.png alt='ProductName Logo'>"){
     $content -replace "<img src=C:\\Program Files (x86)\\ProductName\\.\\ProductNameConfigs\\ProductNameOLDlogo.png alt='ProductName Logo'>","<img src=../../../ProductNameNew/ProductNameNewLogo.png alt='ProductNameNew Logo'>" | Set-Content -Path $_.FullName
  }}

as i ran and check its not getting updated can someone please Provide me some inputs

CodePudding user response:

There are more characters, like the parenthesis and dots to escape than just the backslash. For your "raw string" to be used as a literal pattern in your -match Regular Expression, this means:

[Regex]::Escape("<img src=C:\Program Files (x86)\ProductName\.\ProductNameConfigs\ProductNameOLDlogo.png alt='ProductName Logo'>")
<img\ src=C:\\Program Files \(x86\)\\ProductName\\\.\\ProductNameConfigs\\ProductNameOLDlogo\.png\ alt='ProductName\ Logo'>

Thus:

...
$Pattern = [Regex]::Escape("<img src=C:\Program Files (x86)\ProductName\.\ProductNameConfigs\ProductNameOLDlogo.png alt='ProductName Logo'>")
if ($content -match $Pattern) { ...

Anyways, it is generally a bad idea to attempt to parse HTML with regular expressions.
Instead you might consider a dedicated HTML parser as the HtmlDocument class:

$Html = @'
    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>HTML Images</h2>
    <p>HTML images are defined with the img tag:</p>
    
    <img src=C:\Program Files (x86)\ProductName\.\ProductNameConfigs\ProductNameOLDlogo.png alt='ProductName Logo'>
    
    </body>
    </html>
'@
function ParseHtml($String) {
    $Unicode = [System.Text.Encoding]::Unicode.GetBytes($String)
    $Html = New-Object -Com 'HTMLFile'
    if ($Html.PSObject.Methods.Name -Contains 'IHTMLDocument2_Write') {
        $Html.IHTMLDocument2_Write($Unicode)
    } 
    else {
        $Html.write($Unicode)
    }
    $Html.Close()
    $Html
}

$Document = ParseHtml $Html
$Document.getElementsByTagName('img') |ForEach-Object {
    if (  $_.nameProp -eq 'ProductNameOLDlogo.png' ) {
        $_.src = [uri]::EscapeDataString('../../../ProductNameNew/ProductNameNewLogo.png')
        $_.alt = 'ProductNameNew Logo'
    }
}

@"
<!DOCTYPE html>
<html>
$($Document.body.outerHtml)
</html>
"@
  • Related