Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: Kill (delete) a folder

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)

    Default Kill (delete) a folder

    hi guys.

    can anyone tell me how to delete or Kill a follder ??

    Files (.xls .doc) i can manage ok with the Kill statement, but it doesn't seem to for with a folder.

    Ps. I wont know the names of the folders, but at this stage I will want to felete all folders after C:\Colonial\*.*

    Thanks for any help here.


  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Sydney, Australia
    Post Thanks / Like
    1 Post(s)
    0 Thread(s)

    Default Re: Kill (delete) a folder

    Look up RmDir in VB help.

  3. #3
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Auckland, New Zealand
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)


    To Kill a Folder you can use the RmDir command, unfortunately this is a remnant of the Old BASIC command based on DOS and now superceded by VB.NET and Scripting such as File System and WMI.
    Using the RMDIR command won't work on a Dir that has files in them.

    Instead try one of the following

    Sub RemoveDir_WithFiles()
    Dim objFso As Object

    On Error GoTo DelErr
    'CAUTION: Will delete entire Folder NO PROMPT
    Set objFso = CreateObject("Scripting.FileSystemObject")

    '// delete subfolders of MyDir
    objFso.DeleteFolder "C:\MyDir\*", True

    Exit Sub
    MsgBox Err.Number & vbCr & _
    End Sub

    Sub DeleteFolder()
    '// An error occurs if you try to use RmDir on a directory or folder containing files.
    '// Use the Kill statement to delete all files before attempting to remove a directory or folder
    '// Error 75 Path/File access Error
    RmDir "C:\MyDir"
    End Sub

    Sub Tester()
    Shell " /c md C:\MyDir"
    End Sub

    Sub WMI_DeleteFolder()
    Dim strComputer As String
    Dim strFolderToRemove As String
    Dim objWMIService As Object
    Dim colFolders As Object
    Dim objFolder As Object
    Dim Ret As Long

    strComputer = "."
    '// Note use of // forward slash
    strFolderToRemove = "C:\\MyDir\\New Folder"

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name = '" & strFolderToRemove & "'")

    For Each objFolder In colFolders

    End Sub
    Kind Regards,
    Ivan F Moala From the City of Sails

Some videos you may like

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