Remove directory and it's contents (files, subdirectories) without using FileSystemObject
Asked Answered
C

3

8

I want to know if it's possible to rewrite this piece of code:

Private Sub PrepareDir(ByVal dir As String)
    Dim fso As New FileSystemObject
    If fso.FolderExists(dir) Then Call fso.DeleteFolder(dir, True)
    Call fso.CreateFolder(dir)
End Sub

With VBA statements: Kill, MkDir, etc. Most "difficult" part of this - remove non-empty directory. With FSO it can be done easily, but how it can be done without FSO?

Centralization answered 20/8, 2014 at 9:55 Comment(5)
Out of curiosity, why can't/don't you want to use FileSystemObject?Pentode
@DavidZemens - I'm betting it was a homework assignment. There's no logical reason someone couldn't simply use CreateObject("Scripting.FileSystemObject").DeleteFolder "x:\myFolder"Football
Yeah that's probably a good point @Football have a +1 on me :)Pentode
@Football Homework? LOL. It was just my personal interest. All homeworks I was done myself when got bc.sc.comp. degree at 2007.Centralization
A good reason not to use FileSystemObject is that it reduces dependencies, which can cause grief. E.g. when running on a Mac. I use traditional Basic functions where possible.Aromatize
A
10

This piece of ccode uses RmDir to remove the Folder. AFAIK, RmDir cannot delete the folder unless it is empty, so we first clear the content in the folder then remove the directory.

Private Sub PrepareDirModified(dirStr As String)
On Error Resume Next
    If Right(dirStr, 1) <> "\" Then dirStr = dirStr & "\"
    Kill dirStr & "*.*" 
    RmDir dirStr
    MkDir dirStr
On Error GoTo 0
End Sub

Hope this helps.

Aun answered 20/8, 2014 at 10:47 Comment(3)
You might want to try to add the checking if the folder is empty already, because if it is empty, Kill would result in not found error.Abandon
@Ans: the On Error Resume Next causes that this error would be ignoredUpdraft
I kind of doubt it would work recursively if there are dirs inside the dirUpdraft
F
15

The OP said they want to rewrite their code "without FSO" but it doesn't make sense.

If the goal is to reduce the amount of code, simply make it a one-liner:

CreateObject("Scripting.FileSystemObject").DeleteFolder "x:\myFolder"

As requested, this permanently removes the folder and it's contents.


More Information:

Football answered 5/10, 2018 at 19:35 Comment(1)
1. Multiplatform solution 2. If someone will write own file class, he can also implement .DeleteFolder method: the use would be "one-liner" too in that case.Musick
A
10

This piece of ccode uses RmDir to remove the Folder. AFAIK, RmDir cannot delete the folder unless it is empty, so we first clear the content in the folder then remove the directory.

Private Sub PrepareDirModified(dirStr As String)
On Error Resume Next
    If Right(dirStr, 1) <> "\" Then dirStr = dirStr & "\"
    Kill dirStr & "*.*" 
    RmDir dirStr
    MkDir dirStr
On Error GoTo 0
End Sub

Hope this helps.

Aun answered 20/8, 2014 at 10:47 Comment(3)
You might want to try to add the checking if the folder is empty already, because if it is empty, Kill would result in not found error.Abandon
@Ans: the On Error Resume Next causes that this error would be ignoredUpdraft
I kind of doubt it would work recursively if there are dirs inside the dirUpdraft
V
0

No need to delete files for Deleting folders. Take the path and search for the sub folders in a loop and that sub folder can be deleted. below is the example :copy both procedures and paste on module

Public Function Delete_Folder(ByVal FldrName As String) Dim fso, FSfolder As Object

Set fso = CreateObject("Scripting.FileSystemObject")
Set FSfolder = fso.GetFolder(Application.DefaultFilePath)' This is My Documents folder path

'You can replace with your original folder path

For Each Folder In FSfolder.SubFolders
    'Debug.Print Folder.Name
    If Folder.Name = FldrName Then
        Folder.Delete
        Exit For
    End If
Next

End Function

Sub test() Delete_Folder "Sub_Folder_Name" End Sub

Vanatta answered 3/7, 2020 at 11:14 Comment(1)
this assumes only one-level deepInteresting

© 2022 - 2024 — McMap. All rights reserved.