Hi Michelle;
I went looking for this not to long ago to rename a couple thousand files at a time to re-organize archive files.
I found parts of code all over the place, beginning with the following.
- Find the Files in a directory and list them in the workbook -
Sub ListAllFiles()
Dim fs As FileSearch, ws As Worksheet, i As Long
Set fs = Application.FileSearch
With fs
.SearchSubFolders = ActiveSheet.Cells(1, 11).Value ' set to true if you want sub-folders included
.FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
.LookIn = ActiveSheet.Cells(1, 10).Value 'modify this to where you want to serach
If .Execute > 0 Then
Set ws = ActiveSheet
For i = 1 To .FoundFiles.Count
ws.Cells(i, 1) = .FoundFiles(i)
Next
Else
MsgBox "No files found"
End If
End With
End Sub
- Rename Files -
Sub RenameFiles()
For R = 1 To Range("A1").End(xlDown).Row
OldFileName = Cells(R, 1).Value
NewFileName = Cells(R, 2).Value
On Error Resume Next
If Not Dir(OldFileName) = "" Then Name OldFileName As NewFileName
On Error GoTo 0
Next
End Sub
Ok, so the first procedure needs to know what directory to search, {read the comments for each line once you copy this back into excel} and it will list all the files in that folder and print them in Column A.
The RenameFiles sub goes through the entire range, taking the old filename value from Column A, and the new filename value from Column B and renames the file(s)
Best of Luck
Regards;
Mike
michelle439731 wrote:
Rename files with VBA
11-Dec-09
Afternoon
I want to rename some files in a folder using vba
Preferable what I would like to do i
a)find file x in folder
b)rename file x to x
Please can you help
Cheers
Michelle
Previous Posts In This Thread:
Submitted via EggHeadCafe - Software Developer Portal of Choice
Professional Active Server Pages 3.0 (Wrox)
http://www.eggheadcafe.com/tutorial...4-b4ea9b62129e/professional-active-serve.aspx