in VBA, can i remove a folder without FileSearch?
MZ Tools makes life easier for the Excel VBA coder
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: in VBA, can i remove a folder without FileSearch?

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Singapore
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    Could anyone tell me how to remove a folder? or how to remove all files in a folder without fileSearch ?
    Thx

  2. #2
    MrExcel MVP Anne Troy's Avatar
    Join Date
    Feb 2002
    Location
    Westwood NJ
    Posts
    2,581
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Well, you can delete all the files in a folder with this line:

    kill "c:foldername*.*"

    But I can't work out how to delete a folder.
    ~Anne Troy

  3. #3
    MrExcel MVP Anne Troy's Avatar
    Join Date
    Feb 2002
    Location
    Westwood NJ
    Posts
    2,581
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Just found it and stole it:

    How to delete a folder (folder must be empty):

    RmDir "NewPrivateFolder"
    ' deletes the subfolder NewPrivateFolder in the active folder
    RmDir "F:My DocumentsNewPrivateFolder"
    ' deletes the subfolder NewPrivateFolder in the folder F:My Documents

    ~Anne Troy

  4. #4
    Board Regular
    Join Date
    Feb 2002
    Location
    Singapore
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    thx.
    In fact i know from the help file that if i delete folder, the folder must be empty. So i still need to use Kill to delete all files in every sub folder first. Is it? No shortcut?

  5. #5
    MrExcel MVP Anne Troy's Avatar
    Join Date
    Feb 2002
    Location
    Westwood NJ
    Posts
    2,581
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I'm not sure how much of a shortcut you need. Ok, so I stole it from here:

    http://www.erlandsendata.no/english/...icexamples.htm

    I figure if he didn't offer it up here when he had a chance, it's not available. Unless, of course, it's in a newer version.


    ~Anne Troy

  6. #6
    Board Regular
    Join Date
    Feb 2002
    Location
    Singapore
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thx, Dreamboat. , I have changed my mind and now i use FileSystemObject to move whole folder to another place instead of delete the original dirty folder.
    You are a very activative and kind-hearted female here

    [ This Message was edited by: Andrew XJ on 2002-03-15 21:17 ]

  7. #7
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Try this then;

    Sub RemoveDir_WithFiles()
    Dim Fs As Object

    On Error GoTo DelErr
    'CAUTION: Will delete entire Folder NO PROMPT
    Set Fs = CreateObject("Scripting.FileSystemObject")
    Fs.DeleteFolder "C:mydir", True

    Exit Sub
    DelErr:
    MsgBox Err.Number & vbCr & _
    Err.Description
    End Sub


    Ivan

  8. #8
    Board Regular
    Join Date
    Feb 2002
    Location
    Singapore
    Posts
    77
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Great!!
    Ivan, it works.Thx!

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com