To Kill a Folder you can use the RmDir command, unfortunately this is a remnant of the Old BASIC command based on DOS and now superceded by VB.NET and Scripting such as File System and WMI.
Using the RMDIR command won't work on a Dir that has files in them.
Instead try one of the following
Sub RemoveDir_WithFiles()
Dim objFso As Object
On Error GoTo DelErr
'CAUTION: Will delete entire Folder NO PROMPT
Set objFso = CreateObject("Scripting.FileSystemObject")
'// delete subfolders of MyDir
objFso.DeleteFolder "C:\MyDir\*", True
Exit Sub
DelErr:
MsgBox Err.Number & vbCr & _
Err.Description
End Sub
Sub DeleteFolder()
'// An error occurs if you try to use RmDir on a directory or folder containing files.
'// Use the Kill statement to delete all files before attempting to remove a directory or folder
'// Error 75 Path/File access Error
RmDir "C:\MyDir"
End Sub
Sub Tester()
Shell "command.com /c md C:\MyDir"
End Sub
Sub WMI_DeleteFolder()
Dim strComputer As String
Dim strFolderToRemove As String
Dim objWMIService As Object
Dim colFolders As Object
Dim objFolder As Object
Dim Ret As Long
strComputer = "."
'// Note use of // forward slash
strFolderToRemove = "C:\\MyDir\\New Folder"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolderToRemove & "'")
For Each objFolder In colFolders
objFolder.Delete
Next
End Sub
Like this thread? Share it with others