Changing file names causes an error

superfb

Active Member
Joined
Oct 5, 2011
Messages
255
Office Version
  1. 2007
Platform
  1. Windows
Hi

i have a macro that changes the names of files in a directory i chose. But i have noticed if i want to name two files the same it causes an error........is there something i can incorporate in the coding to highlight this or to create a second file with the same name?

Code:
Sub RenameFiles()


Dim myPath As String
myPath = Range("G2")


r = 2
Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2))
    Name myPath & Cells(r, 1).Value As myPath & Cells(r, 2).Value
    r = r + 1
Loop




End Sub
 
Hello, the mistake was mine. Even with added MyPath the code will crash when numbers must be added.
So below is the correct one:
Code:
Sub RenameFiles()
    'Creating variables and initializing them
    Dim myPath As String: myPath = Range("G2")
    Dim r As Long: r = 2
    Dim i As Long
    Dim fn As String, ext As String, fn2 As String
    
    If Right(myPath, 1) <> "\" Then myPath = myPath & "\" 'if last character is not backslash - add one
    
    Do Until IsEmpty(Cells(r, 1)) And IsEmpty(Cells(r, 2)) 'Start looping through the cells
        If (Not IsEmpty(Cells(r, 1))) And (Not IsEmpty(Cells(r, 2))) Then 'check if both names are not empty
            fn = Cells(r, 2).Value 'assign the new filename to variable fn
            If Dir(myPath & fn) <> "" Then 'check if file with this name already exists
                ext = Right(fn, InStrRev(fn, ".") - 1) 'getting the file extension
                fn2 = Left(fn, Len(fn) - InStrRev(fn, ".")) 'getting the file name w/o extension
                i = 1 'initialize a counter to add to filename if necessary
                Do Until Dir(myPath & fn2 & " - " & i & "." & ext) = "" 'start checking if such filenames with added number i exist
                    i = i + 1 'increase i until a free name is found
                Loop
                fn = fn2 & " - " & i & "." & ext 'change fn to new filename - with added number
            End If
            Name myPath & Cells(r, 1).Value As myPath & fn 'rename file
            r = r + 1 'count renamed files
        End If
    Loop
End Sub

Your next request is not quite clear. Do you want to rename only selected files from the list?
If this is the case - yes, it can be put in the code. You will need to make an additional column in which you have to put some text or number or anything against the files you want to rename.
Then put a line in the code to check this value and rename only if the value says so. :)
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I think it would be good if the code could also change the names of the files in sub folders
The code can do many things, but the deeper you go, the more you will want to do :).

So clarify exactly what you need (not more, not less) and stick to it. When this is done, move to the next one.
And maybe start a new thread if you are to extend the question.
 
Upvote 0
Thank you for the reply.

Understood about starting a new thread.

What I was trying to explain and apologies if not explained clearly.

What happened was as the macro is changing the names.....if it crashes, it changes half of the file names before crashing. Subsequently the macro can not be re-run.

If I delete the names in the sheet that have been changed. The macro crashes again because the number of files in a folder vs the list of file names to be changed in the macro is less.

Could the coded be edited to change this?

So the code only changes the file names I want.

Eg If there are 10 files in a folder and I want to only change 6....
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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