Rename files vba - Run-time error '53': File not found

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
187
Office Version
  1. 365
Platform
  1. Windows
I am creating a new macro that will go thru all of the files in a group of photos, and if the photo is not duplicate numbered (meaning (0) at the end), then it is adding that (0) to the filename. Basically, we've got a new tech who will NOT adjust his phone settings for photos he takes during testing, and he ends up with several like-named files. The system numbers all files after the first, but then puts that first one out of order at the end. I've got to get this fixed, and am real tired of having to manually rename hundreds of files for this.

The macro I'm using, I copied from another one. It works perfect on my other file, but not on this. I'm wondering if any of my filenames might be the problem. If so, I'm forever doomed to manual changes...

Code:
Sub RenameFiles()

Dim UserFolder As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim FromFile As String
Dim ToFile As String


    Sheets("Input").Select
    
    UserFolder = Range("N4")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(UserFolder)

    For Each objFile In objFolder.Files
    On Error GoTo StopObj
        If objFile.Name = "desktop.ini" Then
        Else
            If Right(objFile.Name, 8) = "Lens.jpg" Then
                FromFile = UserFolder & "\" & objFile.Name
                ToFile = UserFolder & "\" & Left(objFile.Name, Len(objFile.Name) - 4) & "(0).jpg"
                MsgBox FromFile & vbNewLine & vbNewLine & ToFile
                Name FromFile As ToFile
            End If
        End If
StopObj:
    Next

End Sub

Notes:

1. Cell N4 holds the complete directory path, in this case being C:\Users\mmay\Documents\VBA Testing\Test Data\_MB\Set1_HC80\Photos\Specimen 1-2

2. Although my MsgBox reads all the filenames correctly (see below), when the Name As tries to excute, I get a Run-time Error 53 (also see below).

3. Is it possible that the horribly disjointed filenames are what's throwing this whole thing off? The files names have commas, underscores and spaces galore.

Hoping someone can point out a type, or some other stupid mistake I've made along the way, to make this work. Thanking your in advance!


1718130747843.png


1718130816409.png
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Just a thought, but are your files on OneDrive (or SharePoint)?

I know that OneDrive has issues with accessing files with the FileSystemObject.

For example, I have an Excel file (XLSM) that reads/writes (or should do so) files from a folder which is managed by OneDrive (like the "Documents" folder) and I always get error when running from that location but when I run the file from a "non-OneDrive" location (like C:\temp) everything is fine.

Maybe it's the same with your code.
Try putting all files to another location (not the C:\users\ folder) and run it from there.
 
Upvote 0
Just a thought, but are your files on OneDrive (or SharePoint)?

I know that OneDrive has issues with accessing files with the FileSystemObject.

For example, I have an Excel file (XLSM) that reads/writes (or should do so) files from a folder which is managed by OneDrive (like the "Documents" folder) and I always get error when running from that location but when I run the file from a "non-OneDrive" location (like C:\temp) everything is fine.

Maybe it's the same with your code.
Try putting all files to another location (not the C:\users\ folder) and run it from there.
Actually, we run on a network folder. My normal filepath is P:\2024\800 Jobs\2024_0815\Test Data\_MB\Set1_HC80\Photos\Specimen 1-2. With continual fails there, I tried copying it down to my c:\ drive to see if it worked there. It didn't, and gave me the same faile. Posting here, though, I went with the C: drive foldername so I could continue to test there while I manually did the work on the network folder.

And I know about the OneDrive idiocy. Macros don't run there, period. Microsoft lets its stupidity shine...
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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