2

I have a couple folders on my desktop which I used temporarily for a macro. Now, I want to delete them. One is a .zip and one is a regular folder.

Set fso = CreateObject("scripting.filesystemobject")
fso.DeleteFolder unzipPath, True

The above code works without error. The non-.zip folder is deleted without issues. This, however, does not remove the file:

On Error GoTo 0
fso.DeleteFolder zipPath, True

Contrary to the MSDN documentation, this does not create an error, either. After far too much time, I realized I simply needed to use DeleteFile for the .zip, since apparently a .zip is technically a file, rather than a folder. Then both items are deleted successfully.

It works now, but I'm still a bit confused as to why using DeleteFolder did not produce an error, though. The aforementioned documentation specifies

An error occurs if no matching folders are found.

UPDATE: For the sake of testing, I created a standalone sub exclusively to test the DeleteFolder on a .zip. The .zip is unchanged; there is no error returned; the sub just ends.

Just a curious soul wandering the realms of VBA...

ZX9
  • 898
  • 2
  • 16
  • 34
  • Do you have some sort of error handler running that could suppress the error? – Degustaf Jun 03 '15 at 16:11
  • Good question! Unfortunately, it looks like I've actually neglected to put in an error handler into that sub as of yet. – ZX9 Jun 03 '15 at 16:13
  • Error handlers do propagate. So if you have one further up the call stack it could be catching this error. – Degustaf Jun 03 '15 at 16:15
  • 2
    so, is an error handler from a calling sub suppressing the error? remember that vba will go back up the calling tree until it finds an error handler to use. Only if there are no error handlers will it then produce an error. I would suggest putting `On Error Goto 0` in the routine to see if an error actually occurs – SeanC Jun 03 '15 at 16:15
  • I've updated the question. `On Error Goto 0` does not provide an error. – ZX9 Jun 04 '15 at 13:25

1 Answers1

1

This behavior is not limited to .ZIP files (which is what Compressed Folders really are). What is interesting is that the DeleteFile method will not throw an error when attempting to delete an existing folder, and the DeleteFolder method does not error when attempting to delete an existing file. The only way I got the to error was by specifying a non-existent file/folder name.


Yes, it appears MSDN is wrong. Try this:

Sub IO_Error()
    Dim objFSO As FileSystemObject
    Dim strTempDir As String

    Set objFSO = New FileSystemObject
    strTempDir = Environ("Temp")
    Debug.Print strTempDir & "\IO Test"
    objFSO.CreateFolder strTempDir & "\IO Test"
    objFSO.CreateTextFile strTempDir & "\IO Test\IO Test.txt", True

    objFSO.DeleteFolder strTempDir & "\IO Test\IO Test.txt", True   'No errors
    objFSO.DeleteFile strTempDir & "\IO Test", True                 'No errors
    objFSO.DeleteFile strTempDir & "\IO Test\", True                'File not found error

End Sub
Tim
  • 2,701
  • 3
  • 26
  • 47