Does anyone have some GB32 code to delete several files & folders within another folder. Simply deleting files inside of a folder is easy but I also need it to delete all the folders from within that folder as well, many of which are not empty. TIA
From GFA Helpfile: These commands can be used to delete one or more subdirectories, for instance:
KillFile Files "C:\temp\*" // deletes all files in the folder temp but not any subdirectory; KillFile "C:\temp\*" // deletes everything.
Sub DeleteFilesAndSubdirectories(ByVal folderPath As String)
Dim fileSystem As Object Set fileSystem = CreateObject("Scripting.FileSystemObject")
If fileSystem.FolderExists(folderPath) Then Dim folder As Object Set folder = fileSystem.GetFolder(folderPath)
' Delete all files in the folder Dim file As Object For Each file In folder.Files file.Delete Next file
' Delete all subdirectories and their contents Dim subFolder As Object For Each subFolder In folder.SubFolders DeleteFilesAndSubdirectories subFolder.Path subFolder.Delete Next subFolder End If
Set fileSystem = Nothing
To use this code, you can call the DeleteFilesAndSubdirectories subroutine and pass in the path of the folder that you want to delete. For example:
This code uses the Scripting.FileSystemObject to work with the file system. It first checks if the specified folder exists, and then recursively deletes all files and subdirectories within it. Finally, it deletes the original folder itself.