Rename lots of files

m kappler

Registered User.
Local time
Today, 17:43
Joined
Mar 14, 2005
Messages
13
I need to rename files contained in a folder.
The new filenames are held in a table along
with the corresponding old filenames.
Example

Table1
Field1(OldFileName) 100026578.jpg
Field2(NewFileName) Pic6589ST.jpg

Is there any VBA code that can use the
NewFileName to rename the files in the
folder on the hard disk

Many thanks
 
Skea

Nothing happens. Doesn't work
Have you tried it ?
 
Private Sub Command33_Click()
Dim OldFName As String
Dim NewFName As String

OldFName = "c:\kea.txt"
NewFName = "c:\sam.txt"
Name OldFName As NewFName
End Sub
 
Hi Skea

Yes, I saw your post. But I need to reference the new
file names from a field in a table and your example is for
one file only and not hundreds.
 
Look up recordsets.

Open a recordset to the table containing the list of old and new names.

Once the recordset is open, set it to the front of the list with .MoveFirst

In a loop,

Use the Dir function to locate the file under its old name. If it exists, use the NameAs command to do the rename. If it does not exist, skip it.

Keep on doing the .MoveNext method on the recordset. If after the move you have not reached .EOF (recsetvar.EOF = TRUE), jump back to the top of the loop.

When you reach recsetvar.EOF, you are done.
 
Hey,

Just a few bits of code that might help with the recordset business

Code:
Dim rs As DAO.Recordset
Dim db As DAO.Database

'Connect to database
Set db = CurrentDb

'Connect to recordset
Set rs = db.openrecordset("Name of form")

'Check for empty recordset
If rs.EOF Then
    'Use flag to leap
    GoTo end_all
Else
    'Records present, move to beginning
    rs.MoveFirst
End If
 
Code:
Function RenameFiles()
Dim FSys As Object
Dim ThisFolder As Object
Dim AllFiles As Object
Dim file As Object

On Error Resume Next
Set FSys = CreateObject("Scripting.FileSystemObject")
Set ThisFolder = FSys.GetFolder("Your folder Name")
Set AllFiles = ThisFolder.Files

For Each file In AllFiles
    file.Name = NewName 'Get name From Recordset
Next
End Function
 
Last edited:
If you want a fully working example of how to do this then let me know.

Q1: Where will the new name come from IE a combination of a number of fields in the table

Q2: Do you want to update the old name and location in the table.

Q3: Any other info you want to store like date created, File Size Act

If anybody else is interested in how to work with the files and folders let me know and I'll sort an example along with this one also the post code above will also work with files stored on a server as long as you have the correct permissions.


Mick
 

Users who are viewing this thread

Back
Top Bottom