Winston618
Member
The following code intends to rename a file name based on certain prefix of the file, however, when another file with the intended name already exist in the folder, the macro will stop.
I tried to insert "if error" command to skip to the next file but it doesn't work. Can someone help? Thanks!
I tried to insert "if error" command to skip to the next file but it doesn't work. Can someone help? Thanks!
Code:
Sub ReNameFiles_A_(ByVal myDir As String)
Dim sfo As Object, MyFile As Object, myFolder As Object, temp As String, newName, i As Long
Set sfo = CreateObject("Scripting.FileSystemObject")
For Each MyFile In sfo.GetFolder(myDir).Files
If MyFile.Name Like "*NCIR0*_A_*" Then
For i = 4 To 5
newName = ExecuteExcel4Macro("right(substitute('" & myDir & "\[" & MyFile.Name & "]sheet1'!r" & i & "c1,""/"",""""),8)")
If (Not IsError(newName)) Then
If (newName <> vbNullString) * (IsNumeric(newName)) Then Exit For
End If
Next
If IsError(newName) Then newName = GetDate(myDir & "\" & MyFile.Name)
If newName <> "" Then
newName = Replace(newName, "/", "")
temp = Left$(sfo.GetBaseName(MyFile.Name), 10) & newName & _
"." & sfo.GetExtensionName(MyFile.Name)
>>>>> Error stop here Name myDir & "\" & MyFile.Name As myDir & "\" & temp
End If
newName = ""
End If
Next
For Each myFolder In sfo.GetFolder(myDir).subfolders
ReNameFiles_A_ myFolder.Path
Next
End Sub