1

I need to write up a script in PowerShell that will search through a folder, find ONLY HTML files, and replace a certain line of markup that I specify with new markup.

Here is what I have so far:

$filePath = 'C:\Users\bettiom\Desktop\schools\alex\Academics'
$processFiles = Get-ChildItem -Exclude *.bak -Filter *.htm -Recurse -Path $filePath

$query = '<html>'
$replace = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'

foreach ( $file in  $processFiles ) {
     $file | Copy-Item -Destination "$file.bak"

    $arrayData = Get-Content $file
    for ($i=0; $i -lt $arrayData.Count; $i++) {
         if ( $arrayData[$i] -match $query ) {
              $arrayData[$i+1] = $arrayData[$i+1].Replace($query,$replace)
         } else { }
    }
    $arrayData | Out-File $file -Force
}

Everything seems to work up until the foreach loop, it then just doesn't execute pass that line.

Any help given will be much appreciated.

Thanks in Advance.

1 Answer 1

1

You're using pipelines where you shouldn't, and avoid them where they'd actually be beneficial. Try this instead:

$filePath = 'C:\Users\bettiom\Desktop\schools\alex\Academics'

$srch = '<html>'
$repl = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'

Get-ChildItem -Exclude *.bak -Filter *.htm -Recurse -Path $filePath | % {
  $file = $_.FullName
  Copy-Item $file "$file.bak"
  (Get-Content $file) -replace $srch, $repl | Out-File $file -Force
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.