Is their a way I can do something like a Kill statment but on a folder. In other words delete a folder no questions asked, and it does not put it in the recycle bin?
Printable View
Is their a way I can do something like a Kill statment but on a folder. In other words delete a folder no questions asked, and it does not put it in the recycle bin?
Add a FileListBox and a Command Button
Code:Private Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Sub Command1_Click()
For i = 0 To File1.ListCount - 1
DeleteFile (File1.Path & "\" & File1.List(i))
Next
Call RemoveDirectory("c:\windows\desktop\blank\")
End Sub
Private Sub Form_Load()
File1.Path = "c:\windows\desktop\blank"
'replace that with the path of your folder
End Sub
Create two directories with many (300+) files in them.
(Same amount of files, and the exact same files, in the two directories)
Create two programs which delete all the files and the directory.
One with the API, and one with the native Visual Basic commands, Kill and RmDir.
Make them both count how long it took.
Compare! The API is much faster.
(Not that it's wrong to use the VB commands!) :rolleyes:
And, KABOOM to you as well! ;)
another alternative is the FileSystemObject
[code]
'using FileSystemObject to delete a folder
Dim myFso As Object
Set myFso = CreateObject("Scripting.FileSystemObject")
myFso.DeleteFolder "C:\yourfolder"
Set myFso = Nothing
[code]
I dont like to use FSO because that is another DLL you have to distribute,
and Batman:
I would have used pure VB, but as Yonaton said, API is much faster, and if the folder contained a lot of files VB would be too slow,
and anyway, all VB functions are, are API calls in discuise...
BTW: does anybody know how to get a listing of files in a directory, Without using the FileListBox???
[/code]denniswrenn: To list the files you can either use the APIs FindFirstFile and FindNextFile, or use the VB function Dir.
You might prefer the API because it's much much faster... Look in the MSDN, because in this special case, I hate it. :rolleyes:
Here's a Dir example, lists all the files in "C:\Windows" in a NORMAL ListBox called List when you click a button called Command1.
Code:Option Explicit
Private Sub Command1_Click()
Dim sFileName As String
Call List1.Clear
sFileName = Dir("C:\Windows\") ' Initiate search... API equivalent: FindFirstFile
Do
Call List1.AddItem(sFileName)
sFileName = Dir ' Continue search... API equivalent: FindNextFile
Loop Until sFileName = vbNullString ' Returns vbNullString when search is done
End Sub