Skip to content Skip to sidebar Skip to footer

Encode Non-ascii Characters To Html Using Javascript/windows Batch File Hybrid

I need to replicate the exact function this website http://www.unicodetools.com/unicode/convert-to-html.php does in a hybrid Javascript/Windows batch script. I have zero knowledge

Solution 1:

Here it is . Brand new and fresh.

You can pass only the file you want to encode (the result will be printed to the console) or pass input and output file.Examples:

call toHtmlEnt.bat input.txt output.txt
call toHtmlEnt.bat input.txt

Solution 2:

I wrote my own script. It took me a whole day basically scouring the Internet for useful pieces of code I could find and combining them to achieve the effect I wanted.

Save the code below to tohtmlent.bat. Use it from CMD like tohtmlent.bat filename.txt or call it from another batch file like call tohtmlent.bat filename.txt where "filename.txt" is the input file. Output will be displayed in the console so use > if you would like to pipe the output to a file. The input file should strictly be encoded in UTF-8. Output is ANSI. What the script does is it converts all Unicode characters with decimal range 128 and higher to their numeric HTML entity equivalents.

The code is nowhere near elegant considering I am not a programmer and it still has a lot more room for improvement. But hey, it does its job!

@if (@X)==(@Y) @end/*
@echo off
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
*/if (WScript.Arguments.Length < 1 ) {
    WScript.Echo("No file specified.");
    WScript.Quit(0)
}

varinputFile= WScript.Arguments.Item(0);
var fso= newActiveXObject("Scripting.FileSystemObject");
var inputFile=WScript.Arguments.Item(0);

if (!fso.FileExists(inputFile)){
    WScript.Echo(inputFile + " does not exist.");
    WScript.Quit(1);
}

varobjAdoS= WScript.CreateObject("ADODB.Stream");
objAdoS.Type = 2;
objAdoS.CharSet = "utf-8";
objAdoS.Open();
objAdoS.LoadFromFile(inputFile);
varstrInput= objAdoS.ReadText();
objAdoS.Close();
varstrOutput='';
for(i=0; i<strInput.length; i++){
    if(strInput.charCodeAt(i)>127){ strOutput += '&#' + strInput.charCodeAt(i) + ';'; }else{ strOutput += strInput.charAt(i); }
}
WScript.Echo(strOutput);

Post a Comment for "Encode Non-ascii Characters To Html Using Javascript/windows Batch File Hybrid"