larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 154
|
Post by larrybtoys on Mar 23, 2023 17:20:55 GMT 1
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
|
|
|
Post by (X) on Mar 23, 2023 17:40:45 GMT 1
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.
From ChatGPT:
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 End Sub
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:
DeleteFilesAndSubdirectories "C:\ExampleFolder"
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.
|
|
larrybtoys
Full Member
 
Retired Part Time IT Professional
Posts: 154
|
Post by larrybtoys on Mar 24, 2023 21:36:40 GMT 1
Thank you. I will play with this code.
|
|