VBS- Rename Files With New Names in Loop

荒凉一梦 提交于 2019-12-11 17:23:54

问题


I have this script below which renames the files in a folder to a name I want if the original filename contains a partial string match.

The issue is, there's currently 17 files that need to be renamed and I use the same if statement 17 times to check for the file name match. I am looking to have only one if statement that is looped through all the files.

Here is the code - The file names in the code provided below are NOT the actual filenames I use:

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

回答1:


You can use regular expression to check and get the desired pattern

Sample script

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

I've assumed that you only want to replace the file names which begins with a number and you want to name them in NameNumber pattern. You can modify the script to suit your needs.

Update Sample Script

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

Based on the new filename information, I have used a dictionary to find a file and rename it based on the information in dictionary. Note that since you are not changing the names of His and Her files, you don't really need to worry about them. For my script, I just imagined the files names as HisFile and HersFile for ease.




回答2:


Answer found here:

Rename Many Files With Specific Names

Code:

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


来源:https://stackoverflow.com/questions/49750186/vbs-rename-files-with-new-names-in-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!