article

Kill Folder function with persistence

Email
Submitted on: 2/20/2015 8:41:00 AM
By: Rde (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1723
 
     This is a Kill Folder function with persistence ... It will remove all sub-folders and files and then optionally delete the specified folder ... I found when removing all the files in the temp folder that some locked files would fail and cause it to not continue with the rest of the files ... This function will continue to remove all unlocked files, even after finding locked files. However, if locked files are found, the parent folder will also not get removed.


 
				

Option Explicit
 
Private Declare Function
GetAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpSpec As String) As Long
Private Declare Function
SetAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpSpec As String, ByVal dwAttributes As Long) As Long
 
Private Const
DIR_SEP As String = "\"
Private Const INVALID_FILE_ATTRIBUTES = (-1)
 
 '-----------------------------------------------------
 
 ' This is a Kill Folder function with persistence.
 
 ' It will remove all sub-folders and files and then
 ' optionally delete the specified folder.
 
 ' I found when removing all the files in the temp folder
 ' that some locked files would fail and cause it to not
 ' continue with the rest of the files.
 
 ' This function will continue to remove all unlocked files,
 ' even after finding locked files. However, if locked files
 ' are found, the parent folder will also not get removed.

 
 '-----------------------------------------------------
 
Public Function AddBackslash(sPath As String) As String
  If
Right$(sPath, 1&) = DIR_SEP Then
     AddBackslash = sPath
  Else
     AddBackslash = sPath & DIR_SEP
  End If
End Function
 
 '-----------------------------------------------------
 
Public Function
FolderExists(sPath As String) As Boolean
  Dim
Attribs As Long
  Attribs = GetAttributes(sPath)
  If Not (Attribs = INVALID_FILE_ATTRIBUTES) Then
     FolderExists = ((Attribs And vbDirectory) = vbDirectory)
  End If
End Function
 
 '-----------------------------------------------------
 
Public Function
KillFolder(sSpec As String, Optional ByVal bJustEmptyDontRemove As Boolean) As Boolean
  Dim
sRoot As String, sDir As String, sFile As String
  Dim
iCnt As Long, iIdx As Long
 
  If Not
FolderExists(sSpec) Then Exit Function
 
  ' Add trailing backslash if missing
  sRoot = AddBackslash(sSpec)
  iCnt
= 2& '.' '..'
 
  On Error Resume Next ' Ignore file errors
  sFile = Dir$(sRoot & "*.*", vbNormal)
  Do While LenB(sFile)
     SetAttributes sRoot & sFile, vbNormal
     Kill sRoot & sFile
     sFile = Dir$

  Loop
 
  On Error GoTo HandleIt ' No error should occur in here
  Do: sDir = Dir$(sRoot & "*", vbDirectory)
     For iIdx = 1& To iCnt
        sDir = Dir$
'.' '..' ['fail']
     Next
     If
LenB(sDir) = 0& Then Exit Do
     If
KillFolder(sRoot & sDir & DIR_SEP) Then
     ' Sub-folder is now gone but Dir$ was reset
     ' during recursive call so Do Dir$(..) again

     Else:
iCnt = iCnt + 1&
     ' Kill folder failed (remnant files) so skip
     ' this folder (iCnt + 1) to get the rest

     End If
  Loop
 
  If
bJustEmptyDontRemove = False Then RmDir sRoot ' Errors here if remnants
HandleIt:
  KillFolder = Not FolderExists(sSpec)
End Function
 
 '-----------------------------------------------------
 
 


Other 30 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.