有人可以帮助解释我如何编写一些 vbs 脚本来搜索具有特定字符串的文件并重命名它们吗?
例如,假设我有一个文件夹 c:\test
我想在 c:\test 中搜索每个带有单词 John 的文件,并用单词 Dave 替换...
例如内容是:
john_list.txt auto.john.doc
脚本后:
dave_list.txt auto.dave.doc
你能帮我吗?
谢谢!
解决方案:
Dim sName
Dim fso
Dim fol
' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder("c:\TEST")
' go thru each files in the folder
For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, "john") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "john", "dave")
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!"