Rename file in dir...

User avatar
sal21
PlatinumLounger
Posts: 4374
Joined: 26 Apr 2010, 17:36

Rename file in dir...

Post by sal21 »

In c:\mydir\ have a series of .xls file, similar:

2500-55545.xls
2500-34344.xls
...
2500-9999.xls

Note:
the first 5 character are the same for all files.

I need to rename all file with AAAA-2500-.......xls

in effect insert AAAA- before the name of file... fow to?

final goal:
AAAA-2500-55545.xls
AAAA-2500-34344.xls
...
AAAA-2500-9999.xls

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Rename file in dir...

Post by HansV »

Here you go:

Code: Select all

Sub RenameFiles()
    Const strPath = "C:\MyDir\"
    Dim strFile As String
    strFile = Dir(strPath & "2500-*.xls")
    Do While strFile <> ""
        Name strPath & strFile As strPath & "AAAA-" & strFile
        strFile = Dir
    Loop
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Rename file in dir...

Post by Rudi »

Unless you needed code for reasons, you could have used ReNamer.
Excellent app for renaming files in a jiffy!!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.