0

So I have this code so far:

http://pastebin.com/L4GvLkB1

That regex currently finds any images that DO NOT have alt tags in them.

I want to take it one step further by adding in an alt attribute with the content inside of it, being the name of the image from src attribute.

As always, all advice or help is greatly appreciated.

If you would like to know what the automate function does, here you go:

function automate($school, $query, $replace) {
    $processFiles = Get-ChildItem -Exclude *.bak -Include "*.html", "*.HTML", "*.htm", "*.HTM" -Recurse -Path $school
    foreach ($file in  $processFiles) {
        #$text = Get-Content $file
        $text = Get-Content $file | Out-String
        $text = $text -replace $query, $replace
        $text | Out-File $file -Force -Encoding utf8
    }
}
Matt Bettiol
  • 309
  • 1
  • 3
  • 9

1 Answers1

0

Try something like this:

Get-ChildItem -Exclude *.bak -Include *.html, *.htm -Recurse -Path $school | % {
  $html = New-Object -COM HTMLFile
  $html.Write([IO.File]::ReadAllText($_.FullName))
  $html.getElementsByTagName('img') | % {
    $_.alt = Split-Path -Leaf $_.src
  }
  [IO.File]::WriteAllText($html.documentElement.outerHTML)
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328