VBA code to rename files in a directory

abesimpson

Active Member
Joined
May 3, 2003
Messages
435
I am trying to use the following Access VB code to rename all the files from *.aqi in a directory to *.csv:

Code:
Sub test()
Name "C:\myfolder\*.aqi" As "C:\myfolder\*.csv"
End Sub

The problem is that VBA does not accept wild cards (at least as given here).

Any suggestions?

Thanks

abe
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Research the FileSystemObject (FSO), specifically renaming files using FSO in VBA...

Sincerely,
 
Upvote 0
Then you can use the Dir() command to give you all the *.aqi files one at a time, do the rename on the name given to you by the Dir() command.

Code:
Dim RetVal As Variant
Dim CurrDir As String
CurrDir = CurDir("C:\")     'Save the current folder
ChDir "C:\MyFolder"         'Change folder to desired folder
RetVal = Dir("*.aqi")       'Get first file in folder
Do While Len(Nz(RetVal)) > 0   'Rename until no more files
  Name RetVal As Left(RetVal, Len(RetVal) - 3) & "csv"  'Rename
  RetVal = Dir()            'Get next file to rename
Loop
ChDir CurrDir               'Change CurDir back to original setting

HTH,
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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