Link to home
Start Free TrialLog in
Avatar of openaccount1
openaccount1

asked on

Tranform Special characters to entity Name recursively

Hi,

On one question posted here in EE I asked someone how to change texts to their entity names and jostrander provided the answer. But what we want next is
(1) Make the pages retain their names because currently the page is being renamed to new.html
(2) make the code search and transform for pages recursively or on multiple subfolders and subfolder of subfolders.

Here is the question posted as a reference on how the code works.
https://www.experts-exchange.com/questions/25224676/Convert-Text-to-UTF-8-Format-or-Source-code-format.html

Below is the vb script code and a sample html page.
SourceDir=replace(wscript.scriptfullname,wscript.scriptname,"")

Set oDict=CreateObject("Scripting.Dictionary")

MakeDictionary


Set fso=CreateObject("Scripting.FileSystemObject")
set oFile=fso.OpenTextFile(SourceDir & "original.html",1)
OriginalText=oFile.ReadAll
oFile.close

arrText=split(OriginalText,vbCrLf)

for each line in arrText
        for i = 1 to len(line)
                myChar=mid(line,i,1)
                If asc(myChar)>=160 and asc(myChar)<=255 then
                        newText=newText & oDict.Item(myChar)
                Else
                        newText = newText & myChar
                End If
                myChar=""
        next
        newText=newText & vbCrLf
next

set newFile=fso.OpenTextFile(SourceDir & "new.html",2,true)
newFile.writeline newText
newFile.close

msgbox "All done!",vbInformation,"Test replace 2"


Sub MakeDictionary
        oDict.Add "¡","&iexcl;"
        oDict.Add "¢","&cent;"
        oDict.Add "£","&pound;"
        oDict.Add "¤","&curren;"
        oDict.Add "¥","&yen;"
        oDict.Add "¦","&brvbar;"
        oDict.Add "§","&sect;"
        oDict.Add "¨","&uml;"
        oDict.Add "©","&copy;"
        oDict.Add "ª","&ordf;"
        oDict.Add "«","&laquo;"
        oDict.Add "¬","&not;"
        oDict.Add "","&shy;"
        oDict.Add "®","&reg;"
        oDict.Add "¯","&macr;"
        oDict.Add "°","&deg;"
        oDict.Add "±","&plusmn;"
        oDict.Add "²","&sup2;"
        oDict.Add "³","&sup3;"
        oDict.Add "´","&acute;"
        oDict.Add "µ","&micro;"
        oDict.Add "¶","&para;"
        oDict.Add "·","&middot;"
        oDict.Add "¸","&cedil;"
        oDict.Add "¹","&sup1;"
        oDict.Add "º","&ordm;"
        oDict.Add "»","&raquo;"
        oDict.Add "¼","&frac14;"
        oDict.Add "½","&frac12;"
        oDict.Add "¾","&frac34;"
        oDict.Add "¿","&iquest;"
        oDict.Add "×","&times;"
        oDict.Add "÷","&divide;"
        oDict.Add "À","&Agrave;"
        oDict.Add "Á","&Aacute;"
        oDict.Add "Â","&Acirc;"
        oDict.Add "Ã","&Atilde;"
        oDict.Add "Ä","&Auml;"
        oDict.Add "Å","&Aring;"
        oDict.Add "Æ","&AElig;"
        oDict.Add "Ç","&Ccedil;"
        oDict.Add "È","&Egrave;"
        oDict.Add "É","&Eacute;"
        oDict.Add "Ê","&Ecirc;"
        oDict.Add "Ë","&Euml;"
        oDict.Add "Ì","&Igrave;"
        oDict.Add "Í","&Iacute;"
        oDict.Add "Î","&Icirc;"
        oDict.Add "Ï","&Iuml;"
        oDict.Add "Ð","&ETH;"
        oDict.Add "Ñ","&Ntilde;"
        oDict.Add "Ò","&Ograve;"
        oDict.Add "Ó","&Oacute;"
        oDict.Add "Ô","&Ocirc;"
        oDict.Add "Õ","&Otilde;"
        oDict.Add "Ö","&Ouml;"
        oDict.Add "Ø","&Oslash;"
        oDict.Add "Ù","&Ugrave;"
        oDict.Add "Ú","&Uacute;"
        oDict.Add "Û","&Ucirc;"
        oDict.Add "Ü","&Uuml;"
        oDict.Add "Ý","&Yacute;"
        oDict.Add "Þ","&THORN;"
        oDict.Add "ß","&szlig;"
        oDict.Add "à","&agrave;"
        oDict.Add "á","&aacute;"
        oDict.Add "â","&acirc;"
        oDict.Add "ã","&atilde;"
        oDict.Add "ä","&auml;"
        oDict.Add "å","&aring;"
        oDict.Add "æ","&aelig;"
        oDict.Add "ç","&ccedil;"
        oDict.Add "è","&egrave;"
        oDict.Add "é","&eacute;"
        oDict.Add "ê","&ecirc;"
        oDict.Add "ë","&euml;"
        oDict.Add "ì","&igrave;"
        oDict.Add "í","&iacute;"
        oDict.Add "î","&icirc;"
        oDict.Add "ï","&iuml;"
        oDict.Add "ð","&eth;"
        oDict.Add "ñ","&ntilde;"
        oDict.Add "ò","&ograve;"
        oDict.Add "ó","&oacute;"
        oDict.Add "ô","&ocirc;"
        oDict.Add "õ","&otilde;"
        oDict.Add "ö","&ouml;"
        oDict.Add "ø","&oslash;"
        oDict.Add "ù","&ugrave;"
        oDict.Add "ú","&uacute;"
        oDict.Add "û","&ucirc;"
        oDict.Add "ü","&uuml;"
        oDict.Add "ý","&yacute;"
        oDict.Add "þ","&thorn;"
        oDict.Add "ÿ","&yuml;"
End Sub

Open in new window

original.html
ASKER CERTIFIED SOLUTION
Avatar of jostrander
jostrander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot a line.  Please add this line to the top of that script:
Dim strFileExtension

Avatar of openaccount1
openaccount1

ASKER

Great! Solution is working the first time. Very much thanks for the help.