Results 1 to 16 of 16

Thread: Kill files underneath a folder?

  1. #1

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    How do you delete all files underneath a folder but not the folder?

  2. #2
    Dreamlax
    Guest
    Get the folders name, delete the folder, and then create a new folder with a same name.

  3. #3

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    o.k i've got:

    srce = "c:\test"
    Kill (srce)
    End Sub

    But that don't work coz it's a folder!
    How should it be?

  4. #4
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    Try this:

    Code:
    Private Sub Command1_Click()
    DeleteFiles ("c:\tmp\tmp")
    End Sub
    
    
    Function DeleteFiles(Path As String)
        Dim File As String
        If Right(Path, 1) <> "\" Then Path = Path & "\"
        If Len(Dir$(Path, vbDirectory)) <> 0 Then
            File = Dir$(Path, vbNormal + vbHidden + vbSystem)
            Do While Len(File)
                MsgBox File
                Kill (Path & File)
                File = Dir
            Loop
        End If
    End Function

  5. #5
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    If you want to delete the folder without fso including subdirectories and optionally send to the recycle bin:

    In bas:
    Code:
    Option Explicit
    
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type
    
    Private Const FOF_NOCONFIRMATION = &H10
    Private Const FO_DELETE = &H3
    Private Const FOF_ALLOWUNDO = &H40
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    Public Function jKill(Path As String, Optional Recycle As Boolean = False) As Boolean
    'By James Mayo
    'Delete file Or Folder And optionally send To recycle bin
    If Len(Dir$(Path)) <> 0 Or Len(Dir$(Path, vbDirectory)) <> 0 Then
        Dim SHFileOp As SHFILEOPSTRUCT
        With SHFileOp
            'Delete the file
            .wFunc = FO_DELETE
            'Select the file
            .pFrom = Path
            .fFlags = FOF_NOCONFIRMATION
            'Allow 'move To recycle bn'
            If Recycle Then .fFlags = .fFlags Or FOF_ALLOWUNDO
        End With
        'perform file operation
        SHFileOperation SHFileOp
        jKill = True
    End If
    End Function

    For example, killing directory and sending to recycle bin:
    Code:
    Private Sub Command1_Click()
    jKill "c:\tmp\tmp", True
    End Sub

  6. #6
    Dreamlax
    Guest
    You don't necessarily have to use Kill to delete the folder. You could use:

    Code:
    Declare Function RemoveDirectory Lib "kernel32.dll" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long 
    
    Private Sub Command1_Click()
    Dim strDirName As String
    strDirName = "C:\WINDOWS"
    RemoveDirectory strDirName
    MkDir strDirName
    End Sub
    Very quick and easy. Just remember to change the directory name....

    I think mine is a lot easier James!!!

  7. #7

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Dreamlx:

    I have a problem a get an error "Path/File access error"
    why is this?

    I'll try jamesm's !
    thanks

  8. #8

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Dreamlax I have this so far:
    Private Sub Command2_Click()
    srce = "c:\test"

    Dim strDirName As String
    strDirName = srce
    RemoveDirectory strDirName
    MkDir strDirName
    end sub

  9. #9
    Dreamlax
    Guest
    Remeber to put the API call in your form or module!

    Code:
    'Place the following line in the General Declarations, or if you put it in a module, make it public.
    Private Declare Function RemoveDirectory Lib "kernel32.dll" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long
    
    Private Sub Command2_Click() 
    Dim srce As String
    srce = "c:\test" 
     
    RemoveDirectory srce
    MkDir srce
    End Sub

  10. #10

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Cool thanks jamesm and dreamlax!
    Works a charm

  11. #11
    Dreamlax
    Guest
    Whose one? Mine or his?

  12. #12

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    both!

  13. #13
    Dreamlax
    Guest
    Phew, I was about to throw an aggro because it works on my computer and I deleted the folder with all the games in it by mistake. There was only 3 really old 486 gamesa so I'm not mad.

  14. #14
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    Dreamlax,

    RemoveDirectory API only works where the folder contains no subdirectories and no files, else it will fail. Why not just use rmdir instead? Additionally it has no ability to send to recycle bin.
    Last edited by JamesM; Apr 11th, 2001 at 11:00 PM.

  15. #15

    Thread Starter
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Yep just found that out!

  16. #16
    Dreamlax
    Guest
    strange, it deleted the games folder. Unless that was your code.

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