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: 2
  • 1663239347923.png
    1663239347923.png
    102.6 KB · Views: 2

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What is the path to the folder that these *.sav files are located in?
 
Upvote 0
After the files are renamed, you will leave them there & get 7 more files the next day that will need to be renamed?
 
Upvote 0
no - i always move them today's files to another folder - dont need macro from that. So Downloads is only a destination folder for the spss files when i get them , then they are moved, after i rename them.
 
Upvote 0
Doesn't matter, I coded you something that won't matter if the renamed files are left there.

Try the following on a sample folder to verify that it works as you want:

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, 10, 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
Okay i did try it - took a couple of tries , but you actually set the directory from the start which threw me off :D
Anyways - it seems to have problems renaming the (1) , (2) ... files - only renamed the panel_data_xxxx_xx_xx one which is AU

screenshot -

1663276482114.png
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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