How to run PowerShell, as a Batch Process file txt/html with REGEX (search and replace)?

Suzana Eree 811 Reputation points
2021-03-10T16:24:19.33+00:00

hello. I have more regex to run on multiple html files from Folder1. I must run more REGEX with search and replace, for example:

SEARCH: (?-s)(".+?") REPLACE BY: $0
SEARCH: (^.*?)=(.*$) Replace by: \1\r\n\2
SEARCH: ^.(.*)$ REPLACE BY: \1

I mage a PowerShellp script, I add those 3 regex search and replace formulas, but is not working. Can anyone help me?

$sourceFiles = Get-ChildItem 'c:\Folder1'  
$destinationFolder = 'c:\Folder1'
foreach ($file in $sourceFiles) {
$sourceContent = Get-Content $file.FullName -Raw

$contentToInsert = [regex]::match($sourceContent,"(?-s)(".+?")").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '$0',$contentToInsert

$contentToInsert = [regex]::match($sourceContent,"(^.*?)=(.*$)").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '\1\r\n\2',$contentToInsert

$contentToInsert = [regex]::match($sourceContent,"^.(.*)$").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '\1',$contentToInsert

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8
} #end foreach file
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,560 questions
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-03-13T21:42:55.037+00:00

    got it. This is the solution:

    $path = 'c:\Folder1\file1.html'
    $result = 'c:\Folder1\result.html'
    Get-Content -Path $path | ForEach-Object{ 
        $one = $_ -replace '(?<=<li>)\s+','CARPET' #replace First Regex with the word CARPET
        $two = $one -replace 'CARPET','DOOR' #replace the word CARPET with DOOR
        ($three = $two -replace 'DOOR','BEAUTIFUL') | Out-File -FilePath $result -Append #replace the word DOOR with BEAUTIFUL
        "Final = $three"
    }
    
    0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Suzana Eree 811 Reputation points
    2021-03-13T14:13:41.16+00:00

    Ok, so I have this lines on File1.html

    <ul id="sidebarNavigation">
    <li><a href="https://mywebsite.com/page-1.html"; title="Page 1">Page 1 (34)</a></li>
    <li> <a href="https://mywebsite.com/page-2.html"; title="Page 2">Page 2 (29)</a></li>
    <li><a href="https://mywebsite.com/page-3.html"; title="Page-3">Page 3 (11)</a></li>
    </ul>

    @Rich Matheisen I made an update on your last code, just for using 2 regex formulas

    $path = 'c:\Folder1\file1.html'  
    $result = 'c:\Folder1\result.html'  
    Get-Content -Path $path | ForEach-Object{   
           $one = ($_ -replace '(?<=<li>)\s+','CARPET')   | Out-File -FilePath $result -Append      
           ($two = $one -replace 'CARPET','DOOR')   | Out-File -FilePath $result -Append   
    'Final = $two'  
         }  
    

    The PowerShell code seems to be good, but the problem is that the second regex, on the $two doesn't make the replacement from CARPET to DOOR. I don't know why.


  2. Suzana Eree 811 Reputation points
    2021-03-13T16:12:40.137+00:00

    nope. It duplicates my lines. This is how the output should look like result.html:

    <ul id="sidebarNavigation">
    <li><a href="https://mywebsite.com/page-1.html"; title="Page 1">Page 1 (34)</a></li>
    <li>DOOR<a href="https://mywebsite.com/page-2.html"; title="Page 2">Page 2 (29)</a></li>
    <li><a href="https://mywebsite.com/page-3.html"; title="Page-3">Page 3 (11)</a></li>
    </ul>

    P.S. in Powershell screen/terminal looks good. But in the result.html it doesn't look good.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.