使用VBS搜索关键字并重命名整个文件

时间:2015-03-20 23:32:44

标签: vbscript

我花了很多时间环顾四周。我确实找到了一种非常接近我所寻找的方法,但它取代了关键字。

Dim sName
Dim fso
Dim fol

' create the filesystem object
 Set fso = WScript.CreateObject("Scripting.FileSystemObject")

  ' get current folder
  Set fol = fso.GetFolder("F:\Downloads")

   ' 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, "[wizardry] tv show bob - 13") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, "[wizardry] tv show bob - 13", "tv show bob S03E13")
' rename the file
fil.Name = sName
End If
Next

 ' echo the job is completed
 WScript.Echo "Completed!"

但正如我所说,唯一的问题是它重新定位关键字。我希望它用我想要的替换ENTIRE文件名。

大多数文件都会像之前一样有一个组标签:[wizardy] tv show bob - 13

我想确保组标签不见了,所以我可以实际复制文件。除非有办法拉出我重命名的当前文件的文件名。

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我认为,您需要使用原始扩展名生成新名称,而不是替换吗?如果没有,请提供更多详细信息。

Dim sName
Dim fso
Dim fol
Dim fil
Dim ext

Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("F:\Downloads")

For Each fil In fol.Files
    'may need to specify a comparison
    If InStr(1, fil.Name, "[wizardry] tv show bob - 13", vbTextCompare) <> 0 Then
        ext = fso.GetExtensionName(fil)
        If Len(ext) > 0 Then ext = "." & ext
        sName = "tv show bob S03E13" & ext
        fil.Name = sName
    End If
Next

WScript.Echo "Completed!"
相关问题