Kill (delete) a folder

mnmhenry

Board Regular
Joined
Mar 28, 2002
Messages
169
hi guys.

can anyone tell me how to delete or Kill a follder ??

Files (.xls .doc) i can manage ok with the Kill statement, but it doesn't seem to for with a folder.

Ps. I wont know the names of the folders, but at this stage I will want to felete all folders after C:\Colonial\*.*

Thanks for any help here.

mark. (y)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
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


<pre>Sub RemoveDir_WithFiles()
<FONT COLOR="#00007F">Dim</FONT> objFso <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>

<FONT COLOR="#00007F">On</FONT> <FONT COLOR="#00007F">Error</FONT> <FONT COLOR="#00007F">GoTo</FONT> DelErr
<FONT COLOR="#007F00">'CAUTION: Will delete entire Folder NO PROMPT</FONT>
<FONT COLOR="#00007F">Set</FONT> objFso = CreateObject("Scripting.FileSystemObject")

<FONT COLOR="#007F00">'// delete subfolders of MyDir</FONT>
objFso.DeleteFolder "C:\MyDir\*", <FONT COLOR="#00007F">True</FONT>

<FONT COLOR="#00007F">Exit</FONT> <FONT COLOR="#00007F">Sub</FONT>
DelErr:
MsgBox Err.Number & vbCr & _
Err.Description
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>

<FONT COLOR="#00007F">Sub</FONT> DeleteFolder()
<FONT COLOR="#007F00">'// An error occurs if you try to use RmDir on a directory or folder containing files.</FONT>
<FONT COLOR="#007F00">'// Use the Kill statement to delete all files before attempting to remove a directory or folder</FONT>
<FONT COLOR="#007F00">'// <FONT COLOR="#00007F">Error</FONT> 75 Path/File access <FONT COLOR="#00007F">Error</FONT></FONT>
RmDir "C:\MyDir"
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>

<FONT COLOR="#00007F">Sub</FONT> Tester()
Shell "command.com /c md C:\MyDir"
<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>

<FONT COLOR="#00007F">Sub</FONT> WMI_DeleteFolder()
<FONT COLOR="#00007F">Dim</FONT> strComputer <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>
<FONT COLOR="#00007F">Dim</FONT> strFolderToRemove <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">String</FONT>
<FONT COLOR="#00007F">Dim</FONT> objWMIService <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>
<FONT COLOR="#00007F">Dim</FONT> colFolders <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>
<FONT COLOR="#00007F">Dim</FONT> objFolder <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Object</FONT>
<FONT COLOR="#00007F">Dim</FONT> Ret <FONT COLOR="#00007F">As</FONT> <FONT COLOR="#00007F">Long</FONT>

strComputer = "."
<FONT COLOR="#007F00">'// Note use of // forward slash</FONT>
strFolderToRemove = "C:\\MyDir\\New Folder"

<FONT COLOR="#00007F">Set</FONT> objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

<FONT COLOR="#00007F">Set</FONT> colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = <FONT COLOR="#007F00">'" & strFolderToRemove & "'")</FONT>

<FONT COLOR="#00007F">For</FONT> Each objFolder In colFolders
objFolder.Delete
<FONT COLOR="#00007F">Next</FONT>

<FONT COLOR="#00007F">End</FONT> <FONT COLOR="#00007F">Sub</FONT>
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,025
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top