Extend file name

Sarvottam

New Member
Joined
Mar 16, 2018
Messages
3
Hi

I am looking for a macro which will open all files from specified folder and rename files which is just extension to the existing file name.

Example-

If file name is GRIR report – It should be changed as “GRIR report as of 01312018”
If file name is AR aging report – It should be changed as “AR aging report as of 01312018” .

Basically my date will change each month rest file name will be same.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
run: RenameAllFiles
alter the date in the 2nd param to the date you want to use.

Code:
Public Sub RenameAllFiles()
getFilesInDir "c:\temp\", Date
End Sub


Public Sub getFilesInDir(ByVal pvDir, ByVal pvDate)
Dim FSO, oFolder, oFile, oRX
Dim sFile As String
Dim vDat, vTarg, vExt, vPrefix
Dim i As Integer


On Error GoTo errGetFiles


If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
If Not IsDate(pvDate) Then
End If


vDat = Format(pvDate, "mmddyyyy")


Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(pvDir)


For Each oFile In oFolder.Files
   If InStr(oFile.Name, ".xls") > 0 Then   'ONLY xl files
          'open file here
      sFile = oFile
      i = InStrRev(oFile, ".")
      vPrefix = Left(oFile, i - 1)
      vExt = Mid(oFile, i)
      vTarg = vPrefix & " report as of " & vDat & vExt
      
      Name sFile As vTarg
   End If
Next


endit:
Set oFile = Nothing
Set oFolder = Nothing
Set FSO = Nothing
Exit Sub


errGetFiles:
 MsgBox Err.Description, , Err
Resume endit
Resume
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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