WScript 参数上的文本编码 [英] Text encoding on WScript arguments

查看:36
本文介绍了WScript 参数上的文本编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过读取用户列表、模板并创建个性化输​​出的批处理文件来生成邮件配置和个性化签名.大功告成:

I'm trying to generate mail configurations and personalized signatures through a batch file that reads a list of users, a template, and creates a personalized output. That's done and works:

@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 被指示为源文件和目标文件,则可以通过管道传输:

To do the text replacing, I created replacetext.vbs, that allows me to replace a string for oter, and can be piped if stdin and stdout are indicated as the source and target files:

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)

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

The problem is that when I put non-ASCII characters in ANSI/Windows-1250 in the people.lst (Comunicación), while it works and reads them in console, showing them (not converting them) as OEM characters (Comunicaci¾n) when I write the output files, somehow it does convert them transparently, so the output file in Windows shows Comunicaci¾n instead of Comunicación.

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

After much debugging, I've localized the problem in ONLY the arguments (no automatic conversion on the template file).

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

How can I disable said transparent conversion, or convert back the input from ANSI to OEM so the conversion works as intended?

推荐答案

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

The problem is that the cmd.exe works with different code page than cscript.exe/wscript.exe. I have similiar problem in Poland, where cmd.exe works with codepage 852 (I believe this is for compatibility with older MS-DOS programs) and wscript.exe works in Windows' native codepage 1250.

为了解决这个问题,把下面这行放在批处理文件的开头:

To solve the problem, put the following line on the beginning of the batch file:

mode con cp select=1250

这篇关于WScript 参数上的文本编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆