[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] INI文件修改的VBS[示例]

  1. dim r(1),file(1),read(1)
  2. set fso=CreateObject("Scripting.FileSystemObject")
  3. Set RegExp = New RegExp
  4. file(0)="1.txt"
  5. file(1)="2.txt"
  6. n=2
  7. for i = 0 to 1
  8.     if not fso.fileexists(file(i)) then
  9.         n=n-1
  10.         err_msg="'"&file(i)&"' "
  11.     end if
  12. next
  13. if n=0 then
  14.     err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  15. end if
  16. if n<>2 then
  17.     msgbox err_msg&"不存在,请检查vbs代码是否修改正确!",16,"Error":wsh.quit
  18. end if
  19. for i = 0 to 1
  20.     if fso.getfile(file(i)).size=0 then
  21.         n=n-1:a=a+1
  22.         err_msg="'"&file(i)&"' "
  23.     end if
  24. next
  25. if n=0 then
  26.     err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  27. end if
  28. if a=1 then a=empty
  29. if n<>2 then
  30.     msgbox err_msg&"文件内容为空,请检查这"&a&"个"&fso.getextensionname(file(0))&"是否是需要修改的文件!",16,"Error":wsh.quit
  31. end if
  32. n=2
  33. for i = 0 to 1
  34.     read(i)=fso.opentextfile(file(i)).readall
  35. next
  36. With RegExp
  37.     .Global = -1
  38.     .multiline=-1
  39.     .pattern="^(.*)=(.*$)"
  40.     if .test(read(0)) then
  41.         for each match in .execute(read(0))
  42.             r(0)=match.submatches(0)
  43.             r(1)=match.submatches(1)
  44.             .pattern="(^"&r(0)&"=)(.*)$"
  45.             read(1)=.Replace(read(1),"$1"&r(1))
  46.         next
  47.         fso.createtextfile(file(1)).write read(1)
  48.     else
  49.         n=n-1
  50.         err_msg="'"&file(0)&"' "
  51.     end if
  52.     if not .test(read(1)) then
  53.         n=n-1
  54.         err_msg="'"&file(1)&"' "
  55.     end if
  56.     if n=0 then
  57.         err_msg="'"&file(0)&"' 和 '"&file(1)&"' "
  58.     end if
  59.     if n<>2 then
  60.         msgbox "没有在 '"&err_msg&"' 中找到相应匹配!",16,"Error":wsh.quit
  61.     end if
  62. End With
复制代码
声明:
本程序是将样本中每一行对应的数据中末尾=号后面的数据进行更替.
如果你对此代码表示不满意还可以进行修整,添加你需要的代码或者批量修改都行!使用范围不仅限INI文件.
如果你感到日常频繁使用软件或者游戏等,需要大量查找/修改感到烦恼时可以考虑用来一键替换.
1

评分人数

本帖最后由 Nsqs 于 2017-3-22 12:21 编辑

回复 2# codegay

我的方法是一次读入再正则一次写入,不需要按行读,大致的模板,不需要复杂的算法.后期也方便修改

TOP

返回列表