VBS - 重命名许多具有特定名称的文件 [英] VBS - Rename Many Files With Specific Names

查看:44
本文介绍了VBS - 重命名许多具有特定名称的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初在这里发布:

在循环中使用新名称重命名文件

有人提供了一个很好的解决方案:

Dim fso, folder, file, folderName, dict'小路folderName = "X:\Test\3rd Party"'未来文件名Set dict = CreateObject("Scripting.Dictionary")dict.Add "MyFile.xlsx", "Mine.xlsx"dict.Add "YourFile.xlsx", "Yours.xlsx"dict.Add "HisFile.xlsx", "His.xlsx"dict.Add "HersFile.xlsx", "Hers.xlsx"dict.Add "TheirFile.xlsx", "Theirs.xlsx"dict.Add "OurFile.xlsx", "Ours.xlsx"' 创建文件系统对象和文件夹对象Set fso = CreateObject("Scripting.FileSystemObject")设置文件夹 = fso.GetFolder(folderName)' 遍历文件夹中的所有文件,直到找到 searchFileName对于文件夹中的每个文件.Files如果 dict.Exists(file.Name) 那么 file.Name = dict(file.Name)下一个

但唯一的问题是寻找精确的文件名匹配.我原来的帖子在一个循环中包含了 17 个 if 语句,每个 if 语句都使用 InStr 函数来查找部分字符串匹配.

不过,上面的解决方案要简洁得多,因此任何有关它的帮助都会很棒.

也许只是想知道是否真的有更好的方法来做我想做的事情.例如,一旦文件被重命名,它是否会被排除在下一个循环之外?

解决方案

将部分字符串映射到字典中的文件名

Set dict = CreateObject("Scripting.Dictionary")dict.Add "MyFile", "Mine.xlsx"dict.Add "YourFile", "Yours.xlsx"dict.Add "His", "His.xlsx"...

并添加一个嵌套循环,在其中迭代部分文件名并根据当前文件名检查它们:

对于文件夹中的每个文件.FilesFor Each str In dict.Keys如果 InStr(str, file.Name) >0 那么文件名 = dict(str)退出万一下一个下一个

I posted originally here:

Rename Files With New Names in Loop

Someone provided a solution which is great:

Dim fso, folder, file, folderName, dict

'Path
folderName = "X:\Test\3rd Party"

'Future FileName
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "MyFile.xlsx", "Mine.xlsx"
dict.Add "YourFile.xlsx", "Yours.xlsx"
dict.Add "HisFile.xlsx", "His.xlsx"
dict.Add "HersFile.xlsx", "Hers.xlsx"
dict.Add "TheirFile.xlsx", "Theirs.xlsx"
dict.Add "OurFile.xlsx", "Ours.xlsx"

' Create filesystem object and the folder object
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For Each file In folder.Files
    If dict.Exists(file.Name) Then file.Name = dict(file.Name)
Next

But the only issue is that is looks for exact file name match. My original post included like 17 if statements within a loop and each if statement used the InStr function to look for partial string match.

The solution above is much cleaner though, so any help with it would be great.

Perhaps just wondering if there is actually a better way of doing what I want too. For example, once a file is renamed, it's excluded from the next loop through?

解决方案

Map the partial strings to the filenames in your dictionary

Set dict = CreateObject("Scripting.Dictionary")
dict.Add "MyFile", "Mine.xlsx"
dict.Add "YourFile", "Yours.xlsx"
dict.Add "His", "His.xlsx"
...

and add a nested loop where you iterate over the partial filenames and check them against the current filename:

For Each file In folder.Files
    For Each str In dict.Keys
        If InStr(str, file.Name) > 0 Then
            file.Name = dict(str)
            Exit For
        End If
    Next
Next

这篇关于VBS - 重命名许多具有特定名称的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆