none
DeleteFolder does not work after Document has been copied out RRS feed

  • Question

  • What I'm trying to do is create an import routine for Scanned Documents.
    The scanner creates Documents in:
          C:\Users\Public\Pictures\Scanned Documents\<Today's Date>
    I import these one at a time into our Datbase. Once successfully imported the original scanned Document is deleted.  This is all working fine.

    What I want to be able to do is to delete the <Today's Date> Folder itself, once it is empty.
    After a considerable amount of testing, I know what the problem is.  I just don't know how to fix it! 

    I have tried using, for example:
        global_FileSystemObject.DeleteFolder "C:\Users\Public\Pictures\Scanned Documents\2016-01-07"
    and:
        Kill ("C:\Users\Public\Pictures\Scanned Documents\2016-01-07\*.*")  '
        RmDir ("C:\Users\Public\Pictures\Scanned Documents\2016-01-07")

    Both work admirably - until I have looked at the Documents inside:
        "C:\Users\Public\Pictures\Scanned Documents\2016-01-07"
    From that point on I can still delete the Documents - but not the Folder.

    Once I close the application, I can delete the Folder.  So it's not a permissions issue.  It seems that opening a Dialog into the Folder leaves an open connection, which I believe is then preventing the Folder from being deleted.  Problem is - how do I break that connection?

    I have tried using both a FileDialog and a CommonDialog as in:

        'Open FileDialog object as a File Picker dialog box
        '--------------------------------------------------
        Set FileDialog_ScannedDocuments = Application.FileDialog(msoFileDialogFilePicker)

        'Open ScannedDocumentsFolder in FileDialog
        With FileDialog_ScannedDocuments
            .Title = "SELECT - Scanned Document to be Imported"
            .InitialFileName = s_ScannedDocumentsFolder

            If .Show = -1 Then
                'User made a Selection, so:
                For Each s_DocumentSelected_Pathname In .SelectedItems
                    'Only return the first s_DocumentSelected_Pathname
                    If Len(s_DocumentSelected_Pathname) > 0 Then Exit For
                Next s_DocumentSelected_Pathname
            Else
                'User pressed Cancel
                Exit Sub
            End If

        End With

    AND:
       
        'Open CommonDialog in s_ScannedDocumentsFolder
        '----------------------------------------------
        With [CommonDialog - Scanned Document]
           .DialogTitle = "SELECT - Scanned Document to be Imported"
           .Filename = s_ScannedDocumentsFolder
           .Filter = "All Files|*.*"
           .Flags = cdlOFNExplorer
           .ShowOpen
           s_DocumentSelected_Pathname = .Filename
        End With


    Both work fine but appear to leave the s_ScannedDocumentsFolder open.

    Friday, January 8, 2016 7:37 AM

Answers

All replies

  • The folder cannot be deleted if a File in it is open. Have you changed the folder to view hidden items to see if a hidden file is open? like a TMP file perhaps?


    Just takes a click to give thanks for a helpful post or answer.
    Please vote “Helpful” or Mark as “Answer” as appropriate.
    Chris Ward
    Microsoft Community Contributor 2012

    Friday, January 8, 2016 5:10 PM
  • No Hidden files present in Folder.  Remember once I have exited our application I can delete the Folder without problems.

    Our Application enables us to find a scanned document, copy it to another Folder defined by our filing algorithm, and once it exists in the new location, delete the original.  That much is working fine.  I just can't delete the Folder.  I can delete the Folder from the Application before I browse files in it and after I have exited the Application.  So something is holding the Folder open - but what?

    Friday, January 8, 2016 11:16 PM
  • I can reproduce this issue, at least with FileDialog (I didn't test CommonDialog).

    The only workaround that I can think of is to check for empty folders and delete them at startup of the program, before one can open the file dialog.

    Matthias Kläy, Kläy Computing AG

    Saturday, January 9, 2016 1:21 PM
  • Thanks for the idea.  The delete routine will have to look for all empty sub-folders of the "Scanned Documents" Folder in case the User browses to another Sub-Folder before existing the system.  (My current routine only checks the last sub-folder used.) 

    If the User goes back in to import another scanned document and the sub-folder last used is empty, I'll open the "Scanned Documents" Folder  instead.  It should be obvious from the Folder Icons, which ones are empty.

    It's a little more complex than the way I was attempting - but at least it will work.


    Sunday, January 10, 2016 12:53 AM