I have Photos in a folder. All the Photos begin with a number. Then after a space a caption is given. I wish to rename the photos only with the numbers.
Example of file names : 312 Hotel, 456 Park, 634 Car.
But some photos were given same numbers with different captions. 456 Bike.
So, while I rename, I check whether a file with the same number is already there in the folder and then do not replace the existing photo.
But after checking for existence of a photo with the same number, if there is duplication, the code stops at [MyOldFile = Dir] with the error mesage 'invalid procedure call or arguement'
If there is duplication, the code should check other photos. How am I to make the code run? Kindly suggest.
This is the code I have:
Example of file names : 312 Hotel, 456 Park, 634 Car.
But some photos were given same numbers with different captions. 456 Bike.
So, while I rename, I check whether a file with the same number is already there in the folder and then do not replace the existing photo.
But after checking for existence of a photo with the same number, if there is duplication, the code stops at [MyOldFile = Dir] with the error mesage 'invalid procedure call or arguement'
If there is duplication, the code should check other photos. How am I to make the code run? Kindly suggest.
This is the code I have:
Code:
Private Sub RenamePhotosDontReplace()
Dim MyFolder As String
Dim MyOldFile As String
Dim MyFile As String
MyFolder = "D:\FotoRenaming\"
MyOldFile = Dir(MyFolder & "*.jpg")
MyFile = MyOldFile
Do While MyFile <> ""
MyFile = Mid(MyFile, 1, InStr(MyFile, " ") - 1)
If Not Dir(MyFolder & MyFile, vbDirectory) = vbNullString Then
MsgBox "File [" & MyFile & "] already exists"
Else
MsgBox "File [" & MyFile & "] does not exist"
Name MyFolder & MyOldFile As MyFolder & MyFile & ".jpg"
End If
MyOldFile = Dir
MyFile = MyOldFile
Loop
End Sub