标题: [其他] BAT+JScript混合编程替换文件内容(兼容UTF-8、Unicode编码) [打印本页]
作者: DAIC 时间: 2013-11-5 16:16 标题: BAT+JScript混合编程替换文件内容(兼容UTF-8、Unicode编码)
原始需求:
http://www.bathome.net/thread-27183-1-1.html- @set @bathome=1 /*
- @echo off
- dir /b a.txt | cscript.exe -nologo -e:jscript "%~f0"
- pause
- goto :eof
- rem */
-
- // JScript starts from here
- while (!WSH.StdIn.AtEndOfStream) {
- f = WSH.StdIn.ReadLine();
- WSH.StdOut.Write("\rHandling:\t"+f);
- try {
- ChangeContent(f);
- WSH.StdOut.Write("\rSuccess:\n");
- }
- catch(e) {
- WSH.StdOut.Write("\rFail:\n");
- }
- }
-
- function ChangeContent(file) {
- var aso, txt;
- aso = new ActiveXObject("ADODB.Stream");
- aso.Mode = 3;
- aso.Type = 2;
- aso.Charset = "cp437";
- aso.Open();
- aso.LoadFromFile(file);
- txt = aso.ReadText(-1);
- if (txt.indexOf('\x00') > -1) {
- aso.Position = 0;
- aso.Charset = "unicode";
- txt = aso.ReadText(-1);
- }
- txt = txt.replace(/<\d+>/g,"")
- aso.Position = 0;
- aso.WriteText(txt);
- aso.SetEOS();
- aso.SaveToFile(file + ".new", 2);
- aso.Close();
- aso = null;
- return txt;
- }
复制代码
参考代码:
http://bbs.bathome.net/viewthread.php?tid=27060#pid139641
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |