
BAT+Jscript混合編程替換文件內容(兼容UTF8Unicode編碼)原始需求:http://www.bathome.net/thread-27183-1-1.html
參考代碼:http://bbs.bathome.net/viewthread.php?tid=27060#pid139641nclick="copycode($('code0'));">復制代碼
- @set @bathome=1
- // 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;
- }

