Results 1 to 3 of 3

Thread: how will i delete a folder

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    151

    how will i delete a folder

    I want to delete or destroy a folder which has one workbook in it.
    How do i do it?

  2. #2
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: how will i delete a folder

    You have to remove the file first then use RmDir

    VB Code:
    1. Option Explicit
    2. Sub KillFolder()
    3.      '   Folder Name:
    4.     Const szFolderName As String = "FolderNameHere"
    5.      
    6.     Dim wkb As Workbook
    7.     Dim szWkbNames As String
    8.     Dim szOpenWkbNames As String
    9.     Dim i As Long
    10.      
    11.     Dim szThisPath As String
    12.     szThisPath = FixTrailingSeparator(ThisWorkbook.Path)
    13.  
    14.     Dim szProjectPath As String
    15.     szProjectPath = szThisPath & szFolderName
    16.  
    17.     With Application.FileSearch
    18.         .NewSearch
    19.         .SearchSubFolders = False
    20.         .LookIn = szProjectPath
    21.         .FileType = msoFileTypeExcelWorkbooks
    22.         .Execute
    23.          
    24.         If .FoundFiles.Count > 0 Then
    25.              
    26.             For i = 1 To .FoundFiles.Count
    27.                  Kill .FoundFiles(i)
    28.             Next i
    29.            
    30.         End If
    31.        
    32.     End With
    33.        
    34.         RmDir szProjectPath
    35. End Sub
    36.  
    37. Public Function FixTrailingSeparator(Path As String, _
    38.     Optional PathSeparator As String = "\") As String
    39.     Select Case Right(Path, 1)
    40.     Case PathSeparator
    41.         FixTrailingSeparator = Path
    42.     Case Else
    43.         FixTrailingSeparator = Path & PathSeparator
    44.     End Select
    45. End Function

    Or with FSO as posted here

    VB Code:
    1. Sub RemoveDir_WithFiles()
    2. Dim objFSO As Object
    3.  
    4. On Error GoTo DelErr
    5. '// CAUTION: Will delete entire Folder NO PROMPT
    6. Set objFSO = CreateObject("Scripting.FileSystemObject")
    7. objFSO.DeleteFolder "C:\Work\MyNewCrapFolder", True
    8.  
    9. XitProperly:
    10. Set objFSO = Nothing
    11.  
    12. Exit Sub
    13. DelErr:
    14. MsgBox "Error:=" & Err.Number & vbCr & Err.Description
    15. Resume XitProperly
    16.  
    17. End Sub
    Last edited by JustinLabenne; Sep 14th, 2005 at 06:40 AM.
    Justin Labenne
    www.jlxl.net

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: how will i delete a folder

    A third option is to use an API to delete the file.
    VB Code:
    1. Private Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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