Renaming files in a folder

tsmithjr

New Member
Joined
Nov 1, 2013
Messages
5
I have a script that looks at Column A for the old file name and compares it to a bunch of images in a folder, when it finds a match it then looks in Column B for the new file name and renames the files.. That works great except when the new file name is the same as the current and it generates a "Run-time error '58' File already exists"

How do I get it to ignore or rename anyway?

Sub renameFiles()
Dim FSO, objFolder, Folder, LR As Long
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\images") 'here set your directory of images'
LR = [a65536].End(xlUp).Row
For Each file In Folder.Files
For i = 1 To LR
If file.Name Like Cells(i, 1) & ".tif" Then file.Name = Cells(i, 2) & ".tif"
Next i
Next file
End Sub

Thanks
Tom
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If you make the comparison, you cannot avoid the message, so you would need to eliminate the comparison by using an If...Then statement. Something like"
Code:
If file.Name <> Cells(i, 1) & ".tif" Then
'your code
End If
So if you have an exact match up front, it would skip to the next file.
 
Upvote 0
try this
Sub renameFiles()
Dim FSO, objFolder, Folder, LR As Long
Dim strPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\Users\pad71381\Desktop\New folder" ' change path as required
Set Folder = FSO.GetFolder(strPath) 'here set your directory of images'
LR = Range("a1", Range("a" & Rows.Count).End(xlUp)).Row
For Each file In Folder.Files
For i = 1 To LR
If file.Name Like Cells(i, 1) Then
Name strPath & "\" & Cells(i, 1) _
As strPath & "\" & Cells(i, 2)
End If
Next i
Next file
End Sub
 
Upvote 0
try this
Sub renameFiles()
Dim FSO, objFolder, Folder, LR As Long
Dim strPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\Users\pad71381\Desktop\New folder" ' change path as required
Set Folder = FSO.GetFolder(strPath) 'here set your directory of images'
LR = Range("a1", Range("a" & Rows.Count).End(xlUp)).Row
For Each file In Folder.Files
For i = 1 To LR
If file.Name Like Cells(i, 1) Then
Name strPath & "\" & Cells(i, 1) _
As strPath & "\" & Cells(i, 2)
End If
Next i
Next file
End Sub

I'll give them a try on Monday when I'm back in the office. Thank you for the responses.
 
Upvote 0
If you make the comparison, you cannot avoid the message, so you would need to eliminate the comparison by using an If...Then statement. Something like"
Code:
If file.Name <> Cells(i, 1) & ".tif" Then
'your code
End If
So if you have an exact match up front, it would skip to the next file.

Thanks for the reply

So if I would really like to make sure that the name in Column A1 matches the file name before renaming I will be unable to to avoid this? If that is true how do I assure that it doesn't rename the wrong file?
 
Upvote 0
You lost me! Can you put up some examples of the file names, before and after?
 
Upvote 0
I tried this and it did nothing...no error or no files renamed...not sure what to do.

Code:
Sub renameFiles()
Dim FSO, objFolder, Folder, LR As Long
Dim strPath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\Images" ' change path as required
Set Folder = FSO.GetFolder(strPath) 'here set your directory of images'
LR = Range("a1", Range("a" & Rows.Count).End(xlUp)).Row
For Each file In Folder.Files
For i = 1 To LR
If file.Name Like Cells(i, 1) Then
Name strPath & "\" & Cells(i, 1) _
As strPath & "\" & Cells(i, 2)
End If
Next i
Next file
End Sub
 
Upvote 0
You lost me! Can you put up some examples of the file names, before and after?

@JLGWhiz

Ok, so in C:\Images I have tif images...
image1.tif
image2.tif
image3.tif


Then I have a Spreadsheet. If the image name in C:\Images matches the old name (Column A1) then rename to new name (Column B1)
old namenew name
image1.tifimage111.tif
image2.tifimage2.tif
image3.tifimage113.tif

<colgroup><col><col></colgroup><tbody>
</tbody>

My code works fine until it gets to image2 where the "new name" is the same as the old name it generates a "Run-time error '58' File already exists and then the code stops running. I would like it to ignore or move on to image3 that need to be renamed.

Hope that helps!

Thanks for your help
 
Upvote 0
That is the same concept that I orignally determined. So, if you put the If...Then statement Inside your loop just before it tests the existing name, it should just skip over the ones that are an exact match and leave the old name intact. You only want to do something with the ones that don't match, as I understand it. So, all you are doing is telling it that if a file name exists equal to the "new file name", then skip to the next name in column A. I don't see where there would be a problem, but maybe I am still missing something. Maybe the way I wrote the code confused you. I used the If <> so that it would just go on and assign a new name for those which do not match. But the logic says, if it is eqaul then move on.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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