• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Fix run-time error 58 - file already exists

Rui Pires

Member
Hi there,

i have a vba code that works fine, but i got a error 58 when the file exists with the same name .


ElseIf InStr(objFile.Name, ".zip") Then

NewName = G_SNAP SHOT" & Format(Date, "DDMMYYYY") & ".zip"

objFile.Name = NewName


' I got ERROR 58 THE FILE ALREADY EXISTS, HOW DO I FIX IT ,ignore this error ?

Thanks in advance for your help
 
Hi ,

If you just wish to ignore the errors , then see if this works :
Code:
Sub rename_5()
    Dim NewName As String, mydir As String, objFile As Object
    Dim Last_name As String, id As String, b As String, n As String

    mydir = Application.ThisWorkbook.Path
    id = Range("e2").Value
    b = Range("f2").Value
    n = Range("h2").Value

    On Error Resume Next
 
    With CreateObject("Scripting.FileSystemObject")
         For Each objFile In .GetFolder(mydir).Files
             If objFile.Name = id & "_" & "BSC" & n & "_BCF" & b & ".xls" Then
                NewName = "CIQ_BCF" & b & "_" & id & ".xls"
                objFile.Name = NewName
             ElseIf InStr(objFile.Name, ".zip") Then
                NewName = id & "G_SNAP SHOT" & Format(Date, "DDMMYYYY") & ".zip"
                objFile.Name = NewName
                '<Site ID>_2G_OSS Plan_20052015.xml
              ElseIf objFile.Name = id & "_BSC" & n & "_BCF" & b & "_SITE" & ".xml" Then
                NewName = id & "_2G_OSS Plan_" & Format(Date, "DDMMYYYY") & ".xml"
                objFile.Name = NewName
             End If
         Next
    End With

    On Error GoTo 0
 
    MsgBox "The files were renamed!"
End Sub
Narayan
 
Hi ,

If you just wish to ignore the errors , then see if this works :
Code:
Sub rename_5()
    Dim NewName As String, mydir As String, objFile As Object
    Dim Last_name As String, id As String, b As String, n As String

    mydir = Application.ThisWorkbook.Path
    id = Range("e2").Value
    b = Range("f2").Value
    n = Range("h2").Value

    On Error Resume Next

    With CreateObject("Scripting.FileSystemObject")
         For Each objFile In .GetFolder(mydir).Files
             If objFile.Name = id & "_" & "BSC" & n & "_BCF" & b & ".xls" Then
                NewName = "CIQ_BCF" & b & "_" & id & ".xls"
                objFile.Name = NewName
             ElseIf InStr(objFile.Name, ".zip") Then
                NewName = id & "G_SNAP SHOT" & Format(Date, "DDMMYYYY") & ".zip"
                objFile.Name = NewName
                '<Site ID>_2G_OSS Plan_20052015.xml
              ElseIf objFile.Name = id & "_BSC" & n & "_BCF" & b & "_SITE" & ".xml" Then
                NewName = id & "_2G_OSS Plan_" & Format(Date, "DDMMYYYY") & ".xml"
                objFile.Name = NewName
             End If
         Next
    End With

    On Error GoTo 0

    MsgBox "The files were renamed!"
End Sub
Narayan


Hi Narayan

after all works very well :)))

Thanks you for your support
 
@ Rui,

This will also do the same job..

Code:
    With CreateObject("Scripting.FileSystemObject")
        For Each objFile In .GetFolder(mydir).Files
            If objFile.Name = id & "_" & "BSC" & n & "_BCF" & b & ".xls" Then _
                NewName = "CIQ_BCF" & b & "_" & id & ".xls"
            If InStr(objFile.Name, ".zip") Then _
                NewName = id & "G_SNAP SHOT" & Format(Date, "DDMMYYYY") & ".zip"
                '<Site ID>_2G_OSS Plan_20052015.xml
            If objFile.Name = id & "_BSC" & n & "_BCF" & b & "_SITE" & ".xml" Then _
                NewName = id & "_2G_OSS Plan_" & Format(Date, "DDMMYYYY") & ".xml"
               
            If Len(Dir(mydir & "\" & NewName)) = 0 Then objFile.Name = NewName
       
        Next
    End With
 
Back
Top