Search code examples
shellvbscriptjscriptwsh

Current charset of the windows script host


I write script (js or vbs, not significant) with Windows Script Host that creating .bat file.

I want to convert ANSI string to OEM with ADODB.Stream

I may get current OEM code page with Split(CreateObject("WScript.Shell").Exec("cmd /c chcp").StdOut.ReadAll, ":")(1) and then convent it to charset with http://msdn.microsoft.com/en-us/library/windows/desktop/dd317756%28v=vs.85%29.aspx.

How I may get the current ANSI(script) charset?


Solution

  • The Windows codepage can be determined via WMI, by reading the CodeSet property of the Win32_OperatingSystem class.

    There are several ways for reading properties of that class. In batch scripts you'd use the commandline executable wmic:

    wmic os get codeset
    

    In VBScript you could shell out and use the same command, but then you'd have to parse text output to get the actual value. It's better to do it this way:

    Set wmi = GetObject("winmgmts://./root/cimv2")
    
    For Each os In wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
      cs = os.CodeSet
    Next
    
    WScript.Echo cs