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

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

Set WshShell = Create (\"W .Shell\")
WshShell.Run \"cmd /c dir /s/b *.htm > list.htm\",vbHide
W .Sleep 1000

sFile = \"list.htm\"
Set objFSO = Create (\" ing.FileSystem \")
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中的FileSystem 实现批量替换,具体的可以参考这篇文章

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

 PS: 的相关转义字符:

 \"/\" (反斜杠)
 VbCrLf (换行符,用来表示重起一行)
 VbTab (水平制表符)
 Chr(8) (退格符)
  vbCr (回车符)
 \"\'\" (单引号)
/\" - > \"\" (双引号)

收藏 打印