【发布时间】:2013-06-23 11:09:39
【问题描述】:
谁能帮助解释我如何编写一些 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!"
【问题讨论】:
-
只要你的 cmets 不再撒谎,我就会投赞成票(并且 - 可选 - 你透露你将如何处理“johnny”)。
-
“cmets 不再撒谎”是什么意思?我说了什么谎?
-
您不处理下划线和空格,而是处理 johns 和 daves。
-
哈哈,是的,确实,我发布的不是我的解决方案,只是一个我必须拼凑起来的解决方案。我想我不妨为下一个需要帮助的人发帖,这样他们可能会得到比我更多的帮助。
-
感谢大家的帮助