melodramatic
Board Regular
- Joined
- Apr 28, 2003
- Messages
- 187
- Office Version
- 365
- Platform
- 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...
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!
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!