vbscriptnon-latin

How can I save non-Latin characters in a text file via VBScript?


Chinese characters cannot be saved in a text file via VBScript.

The VBScript is in a folder whose name is in Chinese: 视窗. The script will create a text file, in which the current working directory will be shown. The Chinese characters cannot be saved in the file. Windows Script Host says "Error: Invalid procedure call or argument". The error will not arise if the folder name is in English.

Path = CreateObject("WScript.Shell").CurrentDirectory
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Path & "\Testing.txt", 8, True)    
objFile.WriteLine Path
objFile.close

Is it possible for VBScript to save a file path that contains Chinese characters?


Solution

  • The method openTextFile has another optional parameter - format. Its value is 0 by default which opens the file in ASCII format. To save the chinese characters in the file, you can open the file in UNICODE format by specifying the value of the parameter format = -1. Here is the Reference.

    objFso.openTextFile(path,8, true, -1)             '-1 = TriStateTrue = Opens the file as Unicode
    
    path = split(wscript.scriptFullName, wscript.scriptname)(0) & "Testing.txt"
    set objFso = createObject("scripting.filesystemobject")
    set objFile = objFso.openTextFile(path,8, true, -1)
    objFile.write path
    objFile.Close
    set objFile = Nothing
    set objFso = Nothing