备忘:遍历批量替换多目录多文件内容——VBS脚本

 

将以下代码复制到一个文本文档中,并将文本文档的后缀修改为.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

 

 

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

 

PS:VBScript的相关转义字符:

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

你可能感兴趣的:(cmd,File,脚本,文档,VBScript)