Results 1 to 9 of 9

Thread: [RESOLVED] .DeleteFile not working

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    536

    Resolved [RESOLVED] .DeleteFile not working

    In a vbs, I have this sub to delete some files depending on the extension:

    Code:
    Sub DoDelete(TheFolder)
    
    Dim TheFile
    Dim FSO2
    
    Set FSO2 = CreateObject("Scripting.FileSystemObject")
    
    TheFile= TheFolder & "\*.xyz"
    
    WScript.echo TheFile
    
    On Error Resume next
    FSO2.DeleteFile(TheFile), TRUE
    
    Set FSO2 = Nothing
    
    End Sub
    WScript.echo TheFile tells me that TheFile is correct, it is something like this:
    Q:\_X15\__KILLME Icons\*.xyz

    But the FSO2.DeleteFile(TheFile), TRUE does not do anything.

    Ideas?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: .DeleteFile not working

    What error message do you get when you remove or comment out the evil OERN?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: .DeleteFile not working

    remove the on error resume next, maybe it will then tell you some error it found.
    further: why are the brackets around TheFile? this should also work: FSO2.DeleteFile TheFile, TRUE

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: .DeleteFile not working

    Quote Originally Posted by Bonnie West View Post
    the evil OERN?
    oh c'mon don't overreact

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    536

    Re: .DeleteFile not working

    Ok, I tried now:

    Code:
    WScript.echo TheFile
    
    FSO2.DeleteFile TheFile, TRUE
    
    WScript.echo TheFile & " is deleted"
    No error comes up.
    And the second echo doesn't say anything.

    This is all what comes out:

    Code:
    Microsoft (R) Windows Script Host, Version 5.812
    Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
    
    Q:\_X15\__KILLME Icons\*.xyz
    
    ***** script completed - exit code: 0 *****
    Remark:
    The files in question are accessible and deleteable...

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: .DeleteFile not working

    you probably have another on error somewhere in the code that calls the DoDelete Sub.
    the effect is as follows: the calling code turns off error handling, jups into the Sub, the Sub stumbles on an error and immediatly leaves the Sub back to the calling code that Comes after the Sub call.

    On error resume next CAN be evil if you don't know what it is doing like in your case, but it is not evil per se.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    536

    Re: .DeleteFile not working

    Right, there WAS an another On Error Resume Next in the calling code.
    I eliminated it.

    Now I see why DeleteFile fails.
    "File not found."

    Knowing this, I re-introduced the On Error resume next before the DeleteFile.
    And guess what: Now it exactly works as expected.

    Thank you very much - solved.

  8. #8
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: [RESOLVED] .DeleteFile not working

    one more Thing to do: when you turn on on error resume next in a Sub to catch an error there, you should make sure that after the error was cought and before the Sub exits you clear the error either by calling err.clear or on error goto 0.

    the following should demonstrate the issue that can occur if you don't do this:
    Code:
    Sub Main()    
        On Error Resume Next
        
        a = 1
        b = 0
        
        c = b / a
        If Err.Number <> 0 Then
            MsgBox "a is zero"
        End If
            
        c = DoSomething(a, b)
        
        d = b / a
        If Err.Number <> 0 Then
            MsgBox "a is zero!"
        End If
        On Error GoTo 0
    End Sub
    
    Function DoSomething(a, b)
        on error resume next
        DoSomething = a / b
        'Err.Clear '<-- missing!
    End Function
    it might not be a good example as i quickly put this together. the Thing is: the calling code is trapping for Errors, the Sub is trapping for Errors but an error raised by the Sub is passed back to the calling code and the calling code could then think the error it finds was caused by itselfs code instead of the Sub's code.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Posts
    536

    Re: [RESOLVED] .DeleteFile not working

    Err.Clear is for sure a good practice.
    I added it.

    Thank you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width