Solved VBA Statement to Rename (2 Viewers)

SloMO

New member
Local time
Yesterday, 20:24
Joined
Jul 17, 2025
Messages
3
Access 2013 32-bit on Windows 11

I need a command to rename a file within the context of the logic I have created to read thru a directory tree and rename any .jpg files found while reading.

I previously used the code below to read thru a directory tree and delete certain files using 'objFile.Delete'.
I want to use the same logic but instead of deleting a file I want to rename it.

My code:

Option Compare Database
Option Explicit

Sub RenamJPGs(passedDirectory As String, _
passedFileTypeToRename As String, _
returnNumRenamed As Long, _
returnNumScanned As Long)
'
Dim objDictionary As Object
Dim objFSO As Object

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'
ProcessFolder objFSO.GetFolder(passedDirectory), _
objDictionary, _
objFSO, _
passedFileTypeToRename, _
returnNumRenamed, _
returnNumScanned
'
End Sub

Sub ProcessFolder(objFolder As Object, _
objDictionary As Object, _
objFSO As Object, _
passedFileTypeToRename, _
returnNumRenamed, _
returnNumScanned)

Dim wkObjFileNameFirst20 As String
Dim wkObjName As String
Dim wkRename1st As String
Dim wkRename2nd As String
Dim wkLength As Long

Dim objFile As Object
Dim objSubFolder As Object

For Each objFile In objFolder.Files

wkObjName = objFile.Name
'
returnNumScanned = returnNumScanned + 1
'
If InStr(1, wkObjName, passedFileTypeToRename) Then
'
returnNumRenamed = returnNumRenamed + 1
'
wkLength = Len(Trim(wkObjName)) - 4
wkRename1st = Mid(wkObjName, 1, wkLength)
wkRename2nd = wkRename1st & "_R" & Trim(Str(returnNumRenamed)) & ".jpg"
'
Debug.Print returnNumRenamed; " "; wkObjName; " "; objFile.Path; " "; wkRename1st; " "; wkRename2nd
'
'''''''''''''''''''''''''' objFile.Delete
objFile.Rename

End If
'
Next

For Each objSubFolder In objFolder.SubFolders

ProcessFolder objSubFolder, _
objDictionary, _
objFSO, _
passedFileTypeToRename, _
returnNumRenamed, _
returnNumScanned
Next

End Sub

I previously used this code to delete files as I read thru the directory tree. The delete statement was

objFile.Delete

I tried many combinations of objFile.Rename but all got syntax errors.

The new file name is stored in variable ‘wkRename2nd ’, the previous name is stored in ‘wkObjName’
 
Last edited:
...I tried many combinations of objFile.Rename but all got syntax errors.

The new file name is stored in variable ‘wkRename2nd ’, the previous name is stored in ‘wkObjName’
Try changing objFile.Rename to:
Code:
objFile.Name = wkRename2nd
 
Just a suggestion and may not be appropriate if SloMo wants his existing logic but the iteration and renaming would be far easier if SloMo were to use the FileSystemObject for this - ie, the correct tool for the job.
 
Just a suggestion and may not be appropriate if SloMo wants his existing logic but the iteration and renaming would be far easier if SloMo were to use the FileSystemObject for this - ie, the correct tool for the job.
Isn't that what they are doing?
Set objFSO = CreateObject("Scripting.FileSystemObject")
 

Users who are viewing this thread

Back
Top Bottom