Consulting

Results 1 to 3 of 3

Thread: Problem Deleting Folders

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Problem Deleting Folders

    Guys,

    While using a treeview control to manipulate file & folder structures, I am getting a runtime error 70 (Permission denied) when trying to delete a folder. The error only occurs under specific conditions. The application also opens & closes Word documents to change some text within them.

    Scenario 1 (Folder 1 will be deleted)

    Using the treeview on the userform, I add the following structure:

    Folder 1\Folder 2\Folder 3\Test.docx

    I then delete Folder 1 using FSO.DeleteFolder method, which works - all folders are deleted.


    Scenario 2 (Folder 1 will not be deleted)

    I add the same structure:

    Folder 1\Folder 2\Folder 3\Test.docx

    This time I open & close Test.docx to manipulate some text. I then try to delete Folder 1 as before, but this time it fails and I get runtime error 70.

    To pinpoint the error in scenario 2, I tried deleting the file and then the folders individually. I can delete Test.docx and Folder 3, but not Folder 2 - Folder 2 seems to be the problem. This seems to happen no matter how many folders are present (i.e., the problem folder is the one above the folder which contains the edited document). E.g. extending scenario 2, Folder 4 would be the problem:

    Folder 1\Folder 2\Folder 3\Folder 4\Folder 5\Test.docx

    I get the same problem when trying to delete the folder manually, after Test.docx has been opened & closed. When doing it manually I get "Folder Access Denied - you will need to provide administrator permission." The folders were originally stored on the network drive so I moved them locally to see if it changed anything - it didn't.

    Any comments or suggestions would be appreciated.

    Thanks,
    Joe

  2. #2
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    @saidee24

    I am sure this problem is to do with permissions and file owners, I think if you alter your settings it would maybe solve your problem. Unfortunately I can't change my settings as I am not an administrator. The solution below, found on http://www.vb-helper.com/howto_delet...hierarchy.html, doesn't use the FileSystemObject and it works for me:

    Private Sub DeleteDirectory(ByVal strDir As String)
    'Delete this directory and all the files it contains.
    Dim strFileName As String
    Dim colFiles As Collection
    Dim i As Integer
      'Get a list of files it contains.
      Set colFiles = New Collection
      strFileName = Dir$(strDir & "\*.*", vbReadOnly + _
      vbHidden + vbSystem + vbDirectory)
      Debug.Print strFileName
      Do While Len(strFileName) > 0
        If (strFileName <> "..") And (strFileName <> ".") Then
          colFiles.Add strDir & "\" & strFileName
        End If
        strFileName = Dir$()
      Loop
      'Delete the files.
      For i = 1 To colFiles.Count
        strFileName = colFiles(i)
        'See if it is a directory.
        If GetAttr(strFileName) And vbDirectory Then
          'It is a directory. Delete it.
          DeleteDirectory strFileName
        Else
          'It's a file. Delete it.
          SetAttr strFileName, vbNormal
          Kill strFileName
        End If
      Next i
      'Remove the read-only flag if set.
      '(Thanks to Ralf Wolter.)
      SetAttr strDir, vbNormal
      RmDir strDir
    End Sub

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,336
    Location
    Joe,

    I've been away this week. Here is another version of your work-around code:

    Private Sub fcnKillFolderAndFiles_without_FSO(ByVal strDir As String)
    'Delete this directory and all the files it contains.
    Dim strFileName As String
      strFileName = Dir$(strDir & "\*.*", vbReadOnly + vbHidden + vbSystem + vbDirectory)
      Do While Len(strFileName) > 0
        If strFileName <> ".." And strFileName <> "." Then
          If GetAttr(strDir & strFileName) And vbDirectory Then
            'Sub-folder - loop recursively to process process and kill
            fcnKillFolderAndFiles_without_FSO strDir & strFileName & Application.PathSeparator
            Exit Do
          Else
            'File - kill it.
             SetAttr strDir & strFileName, vbNormal
             Kill strDir & strFileName
          End If
        End If
        strFileName = Dir$()
      Loop
      SetAttr strDir, vbNormal
      RmDir strDir
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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