重命名具有特定字符串vbs的文件夹中的文件

时间:2013-06-26 10:43:53

标签: file vba vbscript rename

有人可以帮忙解释我如何编写一些搜索具有特定字符串的文件的vbs脚本并重命名它们吗?

例如,假设我有一个文件夹c:\ test

我想在c:\ test中搜索带有单词John的每个文件,并替换为Dave这个词......

e.g。 内容如下:

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!"

1 个答案:

答案 0 :(得分:3)

要复制到子文件夹,您需要this之类的内容。替换文件名中的文本可以这样做:

f.Name = Replace(f.Name, "john", "dave")
相关问题