2012-03-01 68 views
0

我試圖通過讀取用戶列表,模板並創建個性化輸​​出的批處理文件來生成郵件配置和個性化簽名。這樣做了和工作原理:WScript參數上的文本編碼

@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) 

的問題是,當我把非ASCII字符在ANSI/Windows的1250在people.lst(Comunicación),而它的工作原理,並讀取他們在控制檯上,他們展示(不將它們轉換)作爲OEM字符(Comunicaci¾n),當我寫輸出文件時,它以某種方式透明地轉換它們,所以Windows中的輸出文件sho ws Comunicaci¾n而不是Comunicación

經過很多調試之後,我只在參數中定位了問題(模板文件沒有自動轉換)。

如何禁用所述透明轉換,或將ANSI輸入轉換回OEM以使轉換按預期工作?

回答

1

問題是cmd.execscript.exe/wscript.exe不同的代碼頁工作。我有類似的問題在波蘭,cmd.exe作品與代碼頁852(我相信這是與舊的MS-DOS程序的兼容性)和wscript.exe作品在Windows的本地代碼頁1250

爲了解決這個問題,把下面的行在批處理文件的開頭:

mode con cp select=1250

+0

謝謝!謝謝!它像夢一樣運作! – 2012-03-02 17:53:56

+1

或使用'chcp 1250' – dbenham 2015-08-03 11:32:44