Macro for renaming a certain file type only

Life4Music

New Member
Joined
Sep 15, 2022
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone.
Just joined here :)
Wanted to ask for a macro that renames only certain file types in a folder. I want this for a certain file types, as the files I am looking to rename have the today's date in them, so the name changes everyday.
Example - I have 7 SPSS files (.sav) which I want to rename - screenshot below is how they are downloaded so raw names. You'll notice that they have (1), (2) etc i hope this isnt an issue for the macro?
1663239391597.png


Each of the files corresponds to a certain market - so the one which is panel_data_2022_09_15 - AU , panel_data_2022_09_15 (1) - CN , etc.
I want each file so for instance the (1) file , to be named to CN + apply today's date as such - "_20220915" (no " in the new name)
1663239376047.png


Can someone help me with this? Is it possible to do ?
 

Attachments

  • 1663239331180.png
    1663239331180.png
    102.6 KB · Views: 3
  • 1663239347923.png
    1663239347923.png
    102.6 KB · Views: 2
Is there a space before the (*) in the file name? If so, that would explain the problem.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
This will account for the space, if that is the issue:

VBA Code:
Sub RenameFiles()
'
    Dim PathToFiles                 As String
    Dim FileNameWithExtension       As String
    Dim NewFileNameWithExtension    As String
'
    PathToFiles = "C:\Users\" & Environ("UserName") & "\Downloads\"                                                     ' Set this to the path of the folder to look through, Path needs to end with '\' ;)
'
    FileNameWithExtension = Dir(PathToFiles & "*.sav")                                                                  ' Get first .sav file name with extension
'
    Do While Len(FileNameWithExtension) > 0                                                                             ' Loop to cycle through all files in the specified folder
        If (Right(FileNameWithExtension, 3) = "sav") And (Left(FileNameWithExtension, 11) = "panel_data_") Then         '   If we find a file that we want to rename then ...
            NewFileNameWithExtension = Replace(Mid$(FileNameWithExtension, 12), "_", "")                                '       Remove 'panel_data' and any other '_' from the FileNameWithExtension
'
            If InStr(1, NewFileNameWithExtension, "(") = 0 Then                                                         '       If the NewFileNameWithExtension doesn't contain a '(' then ...
                NewFileNameWithExtension = "AU_" & NewFileNameWithExtension                                             '           Append the identifier to the beginning of the NewFileNameWithExtension
            Else                                                                                                        '       Else ...
                Select Case Mid$(NewFileNameWithExtension, 11, 1)                                                       '           Determine which identifer needs to be appended to NewFileNameWithExtension
                    Case Is = "1": NewFileNameWithExtension = "CN_" & Replace(NewFileNameWithExtension, " (1)", "")
                    Case Is = "2": NewFileNameWithExtension = "DE_" & Replace(NewFileNameWithExtension, " (2)", "")
                    Case Is = "3": NewFileNameWithExtension = "IT_" & Replace(NewFileNameWithExtension, " (3)", "")
                    Case Is = "4": NewFileNameWithExtension = "JP_" & Replace(NewFileNameWithExtension, " (4)", "")
                    Case Is = "5": NewFileNameWithExtension = "ES_" & Replace(NewFileNameWithExtension, " (5)", "")
                    Case Is = "6": NewFileNameWithExtension = "US_" & Replace(NewFileNameWithExtension, " (6)", "")
                End Select
            End If
'
            Name PathToFiles & FileNameWithExtension As PathToFiles & NewFileNameWithExtension                          '       Rename the file to the NewFileNameWithExtension
        End If
'
        FileNameWithExtension = Dir                                                                                     '   Check for additional files to rename
    Loop                                                                                                                ' Loop back
'
    MsgBox "Renaming process complete!"                                                                                 ' Inform the user that script has finished
End Sub
 
Upvote 0
Solution
ah yes - there are spaces infront of the (1) (2) and so on.

so the files are actually panel_data_2022_09_15 (1) ... etc
Which part do i need to change?
 
Upvote 0
I didn't see your second message - yes it worked perfectly with the second macro !
I cannot express how thankful i am , macro is really interesting and cool !
If you ever come to Bulgaria , hit me up on the DMs i will buy you a beer :D
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top