当前位置: 移动技术网 > IT编程>脚本编程>VBScript > VBS脚本实现遍历批量替换多目录多文件内容的代码

VBS脚本实现遍历批量替换多目录多文件内容的代码

2018年12月06日  | 移动技术网IT编程  | 我要评论

有时候我们需要将一个目录与子目录中的多个htm网页文件实现批量替换,这里就为大家分享一下

将以下代码复制到一个文本文档中,并将文本文档的后缀修改为.vbs,直接运行即可

set wshshell = createobject("wscript.shell")
wshshell.run "cmd /c dir /s/b *.htm > list.htm",vbhide
wscript.sleep 1000

sfile = "list.htm"
set objfso = createobject("scripting.filesystemobject")
set ofile = objfso.opentextfile(sfile,1)
do while not ofile.atendofstream
  strline = ofile.readline
  if len(strline) > 0 then
   set file = objfso.opentextfile(strline, 1)
   arylines = file.readall
   file.close
   arylines = replace(arylines, "需要替换的内容", "替换后的内容")
   set file = objfso.opentextfile(strline, 2)
   file.write arylines
   file.close
  end if
loop
ofile.close

objfso.deletefile sfile
set objfso = nothing

cmd /c dir /s/b *.htm > list.htm就是将子目录中所哟的htm文件列出来保存到list.htm文件中。

文件替换就是通过vbs中的filesystemobject实现批量替换,具体的可以参考这篇

如果替换的内容中包含有转义字符的话,需要注意转义一下

 ps:vbscript的相关转义字符:

 "/" (反斜杠)
 vbcrlf (换行符,用来表示重起一行)
 vbtab (水平制表符)
 chr(8) (退格符)
  vbcr (回车符)
 "'" (单引号)
/" - > "" (双引号)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网