WScript参数上的文本编码

时间:2012-03-01 10:12:52

标签: vbscript wsh

我正在尝试通过批处理文件生成邮件配置和个性化签名,该文件读取用户列表,模板并创建个性化输​​出。这已经完成并且有效:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
GOTO begin
:writesignature
cscript //NoLogo replacetext.vbs "[NAME]" %1 signature.html stdout | cscript //NoLogo  replacetext.vbs "[JOB]" %3 stdin stdout | cscript //NoLogo replacetext.vbs "[EMAIL]" %2 stdin signature-%4.html
GOTO :end
:begin
FOR /F "tokens=1,2,3,4 delims=;" %%A IN ('TYPE people.lst') DO CALL :writesignature "%%A" "%%B" "%%C" %%D
:end

要进行文本替换,我创建了replacetext.vbs,它允许我替换oter的字符串,并且如果stdin和stdout被指示为源文件和目标文件,则可以使用管道传输:

CONST ForReading = 1
CONST ForWritting = 2
CONST ForAppending = 8
CONST OpenAsASCII   = false  
CONST OpenAsUnicode = true
CONST OpenAsDefault = -2
Const OverwriteIfExist = true
Const FailIfExist      = false
Const CreateIfNotExist = true
Const FailIfNotExist   = false
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objFILEINPUT = Wscript.StdIn
SET objFILEOUTPUT = Wscript.StdOut
IF (Wscript.Arguments.Count < 2) OR (Wscript.Arguments.Count > 4) THEN
Wscript.Echo "Not enought arguments"
Wscript.Echo "replacetext ""<original>"" ""<replacement>"" "
Wscript.Quit(1 MOD 255)
END IF
IF Wscript.Arguments.Count > 2 THEN
    IF Wscript.Arguments(2) = "stdin" THEN
'       Wscript.Echo "Input: StdIn"
    ELSE
'       Wscript.Echo "Input: " + Wscript.Arguments(2)
        SET objFILEINPUT = objFSO.OpenTextFile(Wscript.Arguments(2), ForReading, OpenAsASCII)
    END IF
    IF Wscript.Arguments.Count = 4 THEN
        IF Wscript.Arguments(3) = "stdout" THEN
'           Wscript.Echo "Output: StdOut"
        ELSE
'           Wscript.Echo "Output: " + Wscript.Arguments(3)
            IF objFSO.FileExists(Wscript.Arguments(3)) THEN
                SET objFILEOUTPUT = objFSO.OpenTextFile(Wscript.Arguments(3), ForWritting, CreateIfNotExist, OpenAsASCII)
            ELSE
                SET objFILEOUTPUT = objFSO.CreateTextFile(Wscript.Arguments(3), OverwriteIfExist, OpenAsASCII)
            END IF
        END IF
    END IF
END IF
strText = objFILEINPUT.ReadAll()
strNewText = Replace(strText, Wscript.Arguments(0), Wscript.Arguments(1))
objFILEOUTPUT.Write(strNewText)
objFILEOUTPUT.Close
objFILEINPUT.Close
Wscript.Quit(0 MOD 255)

问题是,当我在ANSI / Windows-1250中将非ASCII字符放在people.lst(Comunicación)中时,它在工作中并在控制台中读取它们,显示它们(不将它们转换为) OEM字符(Comunicaci¾n)在我写输出文件时,它以某种方式透明地转换它们,因此Windows中的输出文件显示Comunicaci¾n而不是Comunicación

经过大量调试后,我只在参数中定位了问题(模板文件没有自动转换)。

如何禁用所述透明转换,或将输入从ANSI转换回OEM,以便转换按预期工作?

1 个答案:

答案 0 :(得分:1)

问题是cmd.exe使用的代码页与cscript.exe/wscript.exe不同。我在波兰有类似的问题,其中cmd.exe使用代码页852(我相信这是为了与旧的MS-DOS程序兼容)而wscript.exe适用于Windows的本机代码页1250.

要解决此问题,请将以下行放在批处理文件的开头:

mode con cp select=1250