Last active
April 1, 2022 15:01
-
-
Save seiren-naru-shirayuri/871d0d8bcc799b250dd42eaedcbbe72a to your computer and use it in GitHub Desktop.
Convert file encoded in UTF32LE with BOM to UTF16LE with BOM or vice versa.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'This script is released under GLWTPL, visit https://github.com/me-shaon/GLWTPL for details. | |
' | |
' | |
' GLWT(Good Luck With That) Public License | |
' Copyright (c) Everyone, except Author | |
' | |
'Everyone is permitted to copy, distribute, modify, merge, sell, publish, | |
'sublicense or whatever they want with this software but at their OWN RISK. | |
' | |
' Preamble | |
' | |
'The author has absolutely no clue what the code in this project does. | |
'It might just work or not, there is no third option. | |
' | |
' | |
' GOOD LUCK WITH THAT PUBLIC LICENSE | |
' TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND MODIFICATION | |
' | |
' 0. You just DO WHATEVER YOU WANT TO as long as you NEVER LEAVE A | |
'TRACE TO TRACK THE AUTHOR of the original product to blame for or hold | |
'responsible. | |
' | |
'IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
'DEALINGS IN THE SOFTWARE. | |
' | |
'Good luck and Godspeed. | |
Option Explicit | |
Const modeDecode = 1, modeEncode = 2 | |
Const adTypeBinary = 1 | |
Const adTypeText = 2 | |
Dim streamin, streamout, node | |
Dim FileIn, FileOut | |
Dim Mode | |
If WScript.Arguments.Named.Exists("?") Then | |
WScript.StdOut.Write("Convert file from and to UTF32LE with BOM" & vbNewLine & _ | |
vbNewLine & _ | |
"Syntax: UTF32Convert /d|/e FileIn FileOut" & vbNewLine & _ | |
vbNewLine & _ | |
"/d Convert ""FileIn"" from UTF32LE with BOM to UTF16LE with BOM and saved in ""FileOut""" & vbNewLine & _ | |
"/e Convert ""FileIn"" from UTF16LE with BOM to UTF32LE with BOM and saved in ""FileOut""" & vbNewLine & _ | |
vbNewLine) | |
WScript.Quit 0 | |
End If | |
If WScript.Arguments.Named.Exists("d") And WScript.Arguments.Named.Exists("e") Then | |
WScript.StdErr.WriteLine("Conflict switches /d and /e.") | |
WScript.Quit 1 | |
ElseIf Not WScript.Arguments.Named.Exists("d") And Not WScript.Arguments.Named.Exists("e") Then | |
WScript.StdErr.WriteLine("Missing switches /d or /e.") | |
WScript.Quit 1 | |
Else | |
If WScript.Arguments.Named.Exists("d") Then | |
Mode = modeDecode | |
ElseIf WScript.Arguments.Named.Exists("e") Then | |
Mode = modeEncode | |
End If | |
End If | |
If WScript.Arguments.Unnamed.Count < 1 Then | |
WScript.StdErr.WriteLine("Missing parameter FileIn.") | |
WScript.Quit 1 | |
ElseIf WScript.Arguments.Unnamed.Count < 2 Then | |
WScript.StdErr.WriteLine("Missing parameter FileOut.") | |
WScript.Quit 1 | |
Else | |
FileIn = WScript.Arguments.Unnamed(0) | |
FileOut = WScript.Arguments.Unnamed(1) | |
End If | |
On Error Resume Next | |
Set streamin = CreateObject("ADODB.Stream") | |
Set streamout = CreateObject("ADODB.Stream") | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot create COM objects.") | |
WScript.Quit 1 | |
End If | |
Select Case Mode | |
Case modeDecode | |
streamin.Open | |
streamin.Type = adTypeBinary | |
streamin.LoadFromFile FileIn | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & """.") | |
WScript.Quit 1 | |
End If | |
streamin.Position = 4 | |
streamout.Open | |
streamout.Type = adTypeBinary | |
Do Until streamin.EOS | |
streamin.CopyTo streamout, 2 | |
streamin.Position = streamin.Position + 2 | |
Loop | |
streamout.Position = 0 | |
streamout.Type = adTypeText | |
streamout.Charset = "unicode" | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Unknwon encoding specified.") | |
WScript.Quit 1 | |
End If | |
streamout.SaveToFile FileOut | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & """.") | |
WScript.Quit 1 | |
End If | |
WScript.StdOut.WriteLine("""" & FileIn & """ was converted from UTF32LE with BOM to UTF16LE with BOM and saved as """ & FileOut & """.") | |
Case modeEncode | |
Dim char | |
streamin.Open | |
streamin.Type = adTypeBinary | |
streamin.LoadFromFile FileIn | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot open file """ & FileIn & """.") | |
WScript.Quit 1 | |
End If | |
streamout.Open | |
streamout.Type = adTypeBinary | |
Set node = CreateObject("MSXML2.DOMDocument").createElement("Binary") | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot create COM objects.") | |
WScript.Quit 1 | |
End If | |
node.dataType = "bin.hex" | |
node.text = "0000" | |
char = node.nodeTypedValue | |
Do Until streamin.EOS | |
streamin.CopyTo streamout, 2 | |
streamout.Write char | |
Loop | |
streamout.SaveToFile FileOut | |
If err.number <> 0 Then | |
WScript.StdErr.WriteLine("Cannot open file """ & FileOut & """.") | |
WScript.Quit 1 | |
End If | |
WScript.StdOut.WriteLine("""" & FileIn & """ was converted from UTF16LE with BOM to UTF32LE with BOM and saved as """ & FileOut & """.") | |
End Select |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment