我在下面有这个脚本,如果原始文件名包含部分字符串匹配,则会将文件夹中的文件重命名为我想要的名称。
问题是,目前有17个文件需要重命名,我使用相同的if statement
17次来检查文件名是否匹配。我期待只有一个if语句循环遍历所有文件。
以下是代码 - 下面提供的代码中的文件名不是我使用的实际文件名:
Dim fso, folder, file
Dim folderName, searchFileName, renameFile1, renameFile2, renameFile3, renameFile4, renameFile5, renameFile6, renameFile7, renameFile8
' Parameters
'Path
folderName = "X:\Test\3rd Party"
'Future FileName
renameFile1 = "Mine.xlsx"
renameFile2 = "Yours.xlsx"
renameFile3 = "His.xlsx"
renameFile4 = "Hers.xlsx"
renameFile5 = "Theirs.xlsx"
renameFile6 = "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
' See if the file starts with the name we search
if instr (file.Name, "MyFile") then
file.name = renameFile1
End If
if instr (file.Name, "YourFile") then
file.name = renameFile2
End If
if instr (file.Name, "His") then
file.name = renameFile3
End If
if instr (file.Name, "Hers") then
file.name = renameFile4
End If
if instr (file.Name, "TheirFile") then
file.name = renameFile5
End If
if instr (file.Name, "OurFile") then
file.name = renameFile6
End If
Next
答案 0 :(得分:1)
您可以使用regular expression检查并获得所需的模式
示例脚本
Dim fso, folder, file, folderName
Dim objRegEx, objMatch
'Path
folderName = "X:\Test\3rd Party"
' Create filesystem object and the folder object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderName)
' To Regex check if the file name begins with a number
Set objRegEx = CreateObject("VBScript.RegExp")
With objRegEx
.IgnoreCase = True
.Global = True
.Pattern = "^[0-9]+" 'if file name begins with a number
End With
' Loop over all files in the folder until the searchFileName is found
For Each file In folder.Files
'Check if file name begins with number - if yes then rename the file
Set objMatch = objRegEx.Execute(file.Name)
If objMatch.Count = 1 Then file.Name = "Name" & objMatch.Item(0).Value & ".xlsx"
Set objMatch = Nothing
Next
我假设您只想替换以数字开头的文件名,并且您希望以 NameNumber 模式命名它们。您可以修改脚本以满足您的需求。
更新示例脚本
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
根据新的文件名信息,我使用字典查找文件,并根据字典中的信息重命名。请注意,由于您没有更改他和她的文件的名称,因此您不必担心它们。对于我的脚本,我只是想象文件名为HisFile和HersFile以便于使用。
答案 1 :(得分:0)
在此处找到答案:
Rename Many Files With Specific Names
<强>代码:强>
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "MyFile", "Mine.xlsx"
dict.Add "YourFile", "Yours.xlsx"
dict.Add "His", "His.xlsx"
...
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