【问题标题】:rename a file in a folder with a specific string vbs使用特定字符串 vbs 重命名文件夹中的文件
【发布时间】: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。
  • 哈哈,是的,确实,我发布的不是我的解决方案,只是一个我必须拼凑起来的解决方案。我想我不妨为下一个需要帮助的人发帖,这样他们可能会得到比我更多的帮助。
  • 感谢大家的帮助

标签: file vba vbscript rename


【解决方案1】:

要递归到子文件夹,您需要this 之类的东西。替换文件名中的文本可以这样完成:

f.Name = Replace(f.Name, "john", "dave")

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2021-06-21
    • 2012-10-18
    • 2017-10-11
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    相关资源
    最近更新 更多