|
-
Sep 14th, 2005, 12:23 AM
#1
Thread Starter
Addicted Member
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?
-
Sep 14th, 2005, 06:36 AM
#2
Lively Member
Re: how will i delete a folder
You have to remove the file first then use RmDir
VB Code:
Option Explicit
Sub KillFolder()
' Folder Name:
Const szFolderName As String = "FolderNameHere"
Dim wkb As Workbook
Dim szWkbNames As String
Dim szOpenWkbNames As String
Dim i As Long
Dim szThisPath As String
szThisPath = FixTrailingSeparator(ThisWorkbook.Path)
Dim szProjectPath As String
szProjectPath = szThisPath & szFolderName
With Application.FileSearch
.NewSearch
.SearchSubFolders = False
.LookIn = szProjectPath
.FileType = msoFileTypeExcelWorkbooks
.Execute
If .FoundFiles.Count > 0 Then
For i = 1 To .FoundFiles.Count
Kill .FoundFiles(i)
Next i
End If
End With
RmDir szProjectPath
End Sub
Public Function FixTrailingSeparator(Path As String, _
Optional PathSeparator As String = "\") As String
Select Case Right(Path, 1)
Case PathSeparator
FixTrailingSeparator = Path
Case Else
FixTrailingSeparator = Path & PathSeparator
End Select
End Function
Or with FSO as posted here
VB Code:
Sub RemoveDir_WithFiles()
Dim objFSO As Object
On Error GoTo DelErr
'// CAUTION: Will delete entire Folder NO PROMPT
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder "C:\Work\MyNewCrapFolder", True
XitProperly:
Set objFSO = Nothing
Exit Sub
DelErr:
MsgBox "Error:=" & Err.Number & vbCr & Err.Description
Resume XitProperly
End Sub
Last edited by JustinLabenne; Sep 14th, 2005 at 06:40 AM.
-
Sep 14th, 2005, 09:24 PM
#3
Re: how will i delete a folder
A third option is to use an API to delete the file.
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|