Results 1 to 6 of 6

Thread: Deleteing a folder

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313

    Question

    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?

  2. #2
    Guest
    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

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    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!)

    And, KABOOM to you as well!

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Guest
    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???

  6. #6
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    [/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.
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width