Results 1 to 5 of 5

Thread: How to delete folder ?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    How to delete folder ?

    I have testing directory in my computer. My directory is "Workspace". How I can delete all folder in this workspace. This workspace is become rubbish because I use this directory in my application creating new folder. So I want to empty the folders in this directory first before create new folder. How to do that?

  2. #2
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: How to delete folder ?

    add a reference to Microsoft Scripting Runtime to your project, and then use this code:

    Code:
    Dim oFolder As Scripting.Folder
    Dim fso As Scripting.FileSystemObject
    
           
    Set fso = New FileSystemObject
    
    
    If fso.FolderExists("c:\temp\EmailAttachments") Then
               
            fso.DeleteFolder "c:\temp\EmailAttachments" , True  'setting force to true
                            'deletes read-only file
    
    End If
    I havent tested this though.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to delete folder ?

    Try this
    Code:
    Private Sub DeleteFolder(FolderPath)
    Dim MyFile, MyPath, MyName
    Dim Fs As Scripting.FileSystemObject
    Set Fs = CreateObject("Scripting.FileSystemObject")
    ' Display the names that represent directories.
    MyPath = FolderPath   ' Set the path.
    MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
    Do While MyName <> ""   ' Start the loop.
       ' Ignore the current directory and the encompassing directory.
       If MyName <> "." And MyName <> ".." Then
          ' Use bitwise comparison to make sure MyName is a directory.
          If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
             MsgBox MyPath & MyName
             Fs.DeleteFolder (MyPath & MyName)
          End If   ' it represents a directory.
       End If
       MyName = Dir   ' Get next entry.
    Loop
    
    End Sub
    
    Private Sub cmdDeleteFolder_Click()
    Call DeleteFolder("d:\temp\")
    End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: How to delete folder ?

    Sometimes the folder I try to delete is in used .So I can't delete it.
    So How I can ignore this?

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to delete folder ?

    You can't ignore it. You have to wait until it is not in use.

    If you delete something that someone else is currently using, you will generate an error on their end, which, I'm sure, would confuse the heck out of them.

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