找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 21771|回复: 0

[其他] BAT+JScript混合编程替换文件内容(兼容UTF-8、Unicode编码)

[复制链接]
发表于 2013-11-5 16:16:09 | 显示全部楼层 |阅读模式
原始需求:
http://www.bathome.net/thread-27183-1-1.html
  1. @set @bathome=1 /*
  2. @echo off
  3. dir /b a.txt | cscript.exe -nologo -e:jscript "%~f0"
  4. pause
  5. goto :eof
  6. rem */

  7. // JScript starts from here
  8. while (!WSH.StdIn.AtEndOfStream) {
  9.     f = WSH.StdIn.ReadLine();
  10.     WSH.StdOut.Write("\rHandling:\t"+f);
  11.     try {
  12.         ChangeContent(f);
  13.         WSH.StdOut.Write("\rSuccess:\n");
  14.     }
  15.     catch(e) {
  16.         WSH.StdOut.Write("\rFail:\n");
  17.     }
  18. }

  19. function ChangeContent(file) {
  20.     var aso, txt;
  21.     aso = new ActiveXObject("ADODB.Stream");
  22.     aso.Mode = 3;
  23.     aso.Type = 2;
  24.     aso.Charset = "cp437";
  25.     aso.Open();
  26.     aso.LoadFromFile(file);
  27.     txt = aso.ReadText(-1);
  28.     if (txt.indexOf('\x00') > -1) {
  29.         aso.Position = 0;
  30.         aso.Charset = "unicode";
  31.         txt = aso.ReadText(-1);
  32.     }
  33.     txt = txt.replace(/<\d+>/g,"")
  34.     aso.Position = 0;
  35.     aso.WriteText(txt);
  36.     aso.SetEOS();
  37.     aso.SaveToFile(file + ".new", 2);
  38.     aso.Close();
  39.     aso = null;
  40.     return txt;
  41. }
复制代码
参考代码:
http://bbs.bathome.net/viewthread.php?tid=27060#pid139641
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 09:28 , Processed in 0.016923 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表