code to change file name

dennisli

Well-known Member
Joined
Feb 20, 2004
Messages
1,070
Good morning, Guys,

I have lots of Excel files named with dates, such as Paid0604, Premium0604, 0604Part1 for June 2004 under directory C:\0604. For July 2004, I have to change the name to Paid0704, Premium0704, 0704Part1 under directory C:\0704 by manual.

Are there some codes to do this job?

Thanks lot.

Dennis
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Okay, this makes a few assumptions.

  1. Root folder will always be in following format: mmyy (0604,0704,0804)
  2. The root month determines what the new files will be named

To use, run the UpdateFileMonth procedure. It will prompt for your prior month's file. Select the correct folder, and it will process.

Be careful though, there is no error checking for user input errors. Make sure that you select the correct folder before processing.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> BrowseInfo
    hWndOwner <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    pidlRoot <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    sDisplayName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    sTitle <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    ulFlags <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    lpfn <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    lParam <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    iImage <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SHBrowseForFolder <SPAN style="color:#00007F">Lib</SPAN> "Shell32.dll" (bBrowse <SPAN style="color:#00007F">As</SPAN> BrowseInfo) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SHGetPathFromIDList <SPAN style="color:#00007F">Lib</SPAN> "Shell32.dll" (<SPAN style="color:#00007F">ByVal</SPAN> lItem <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> sDir <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> UpdateFileMonth()
    <SPAN style="color:#00007F">Dim</SPAN> oldFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, newFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    
    oldFolder = BrowseForDirectory("Select Prior Month Folder")

    <SPAN style="color:#00007F">If</SPAN> oldFolder <> vbNullString <SPAN style="color:#00007F">Then</SPAN>
        RollFiles oldFolder, Right(oldFolder, 4)
    
        MsgBox "Complete!"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> RollFiles(filePath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, replaceValue <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
    <SPAN style="color:#00007F">Dim</SPAN> fso <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fld <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, subfld <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, f <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> fso = CreateObject("Scripting.FileSystemObject")
    
    <SPAN style="color:#00007F">Set</SPAN> fld = fso.GetFolder(filePath)
    
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    MkDir AddDate(fld.Path, replaceValue)
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> f <SPAN style="color:#00007F">In</SPAN> fld.Files
        FileCopy f.Path, AddDate(f.Path, replaceValue)
    <SPAN style="color:#00007F">Next</SPAN>
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> subfld <SPAN style="color:#00007F">In</SPAN> fld.SubFolders
        RollFiles subfld.Path, replaceValue
    <SPAN style="color:#00007F">Next</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> fld = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> fso = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> AddDate(value <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, searchFor <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> tmp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, tmpReplace <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> tmpDate <SPAN style="color:#00007F">As</SPAN> Date
    tmp = value
    tmpDate = DateValue(Left(searchFor, 2) & "/1/" & Right(searchFor, 2))
    tmpDate = DateAdd("m", 1, tmpDate)
    tmpReplace = Format(tmpDate, "mmyy")
    tmp = Replace(tmp, searchFor, tmpReplace)
    AddDate = tmp
    tmp = vbNullString
    tmpReplace = vbNull<SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> BrowseForDirectory(<SPAN style="color:#00007F">Optional</SPAN> title <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> browse_info <SPAN style="color:#00007F">As</SPAN> BrowseInfo
    <SPAN style="color:#00007F">Dim</SPAN> item <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> dir_name <SPAN style="color:#00007F">As</SPAN> String

   <SPAN style="color:#00007F">With</SPAN> browse_info
       .pidlRoot = 0
       .sDisplayName = Space$(260)
       .sTitle = title
       .ulFlags = 1
       .lpfn = 0
       .lParam = 0
       .iImage = 0
   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>

   item = SHBrowseForFolder(browse_info)
   <SPAN style="color:#00007F">If</SPAN> item <SPAN style="color:#00007F">Then</SPAN>
       dir_name = Space$(260)
       <SPAN style="color:#00007F">If</SPAN> SHGetPathFromIDList(item, dir_name) <SPAN style="color:#00007F">Then</SPAN>
           BrowseForDirectory = Left(dir_name, _
                                InStr(dir_name, Chr$(0)) - 1)
       <SPAN style="color:#00007F">Else</SPAN>
           BrowseForDirectory = ""
       <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
   <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>
</FONT>

This code should be placed in a standard module.
 
Upvote 0
Good afternoon, everyone,
This code is very useful for me for many years but now I got an error message:

Run-time error ‘70’:
Permission denied

And the following code is highlighted:

Code:
        FileCopy f.Path, AddDate(f.Path, replaceValue)

Any ideas? I appreciate your help.

Dennis
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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