<tt> <p nowrap> <nobr> <font color="#000099"> Option Explicit &#160; Private Declare Function</font> <font color="#660000">GetAttributes</font> <font color="#000099">Lib</font> "kernel32" <font color="#000099">Alias</font> "GetFileAttributesA" <font color="#000099">(ByVal</font> <font color="#660000">lpSpec</font> <font color="#000099">As String) As Long Private Declare Function</font> <font color="#660000">SetAttributes</font> <font color="#000099">Lib</font> "kernel32" <font color="#000099">Alias</font> "SetFileAttributesA" <font color="#000099">(ByVal</font> <font color="#660000">lpSpec</font> <font color="#000099">As String, ByVal</font> <font color="#660000">dwAttributes</font> <font color="#000099">As Long) As Long &#160; Private Const</font> <font color="#660000">DIR_SEP</font> <font color="#000099">As String</font> = "\" <font color="#000099">Private Const</font> <font color="#660000">INVALID_FILE_ATTRIBUTES</font> = (-1) &#160; <font color="#000099"> &#160;'-----------------------------------------------------</font> &#160; <font color="#006600"> &#160;' This is a Kill Folder function with persistence. &#160; &#160;' It will remove all sub-folders and files and then &#160;' optionally delete the specified folder. &#160; &#160;' I found when removing all the files in the temp folder &#160;' that some locked files would fail and cause it to not &#160;' continue with the rest of the files. &#160; &#160;' This function will continue to remove all unlocked files, &#160;' even after finding locked files. However, if locked files &#160;' are found, the parent folder will also not get removed.</font> &#160; <font color="#000099"> &#160;'----------------------------------------------------- </font> &#160; <font color="#000099">Public Function</font> <font color="#660000">AddBackslash(sPath</font> <font color="#000099">As String) As String &#160; If</font> <font color="#660000">Right$(sPath, 1&) = DIR_SEP</font> <font color="#000099">Then</font> &#160; &#160; &#160;<font color="#660000">AddBackslash = sPath</font> &#160; <font color="#000099">Else</font> &#160; &#160; &#160;<font color="#660000">AddBackslash = sPath & DIR_SEP</font> &#160; <font color="#000099">End If End Function &#160; &#160;'----------------------------------------------------- &#160; Public Function</font> <font color="#660000">FolderExists(sPath</font> <font color="#000099">As String) As Boolean &#160; Dim</font> <font color="#660000">Attribs</font> <font color="#000099">As Long</font> &#160; <font color="#660000">Attribs = GetAttributes(sPath)</font> &#160; <font color="#000099">If Not</font> <font color="#660000">(Attribs = INVALID_FILE_ATTRIBUTES)</font> <font color="#000099">Then</font> &#160; &#160; &#160;<font color="#660000">FolderExists = ((Attribs</font> <font color="#000099">And</font> <font color="#660000">vbDirectory) = vbDirectory)</font> &#160; <font color="#000099">End If End Function &#160; &#160;'----------------------------------------------------- &#160; Public Function</font> <font color="#660000">KillFolder(sSpec</font> <font color="#000099">As String, Optional ByVal</font> <font color="#660000">bJustEmptyDontRemove</font> <font color="#000099">As Boolean) As Boolean &#160; Dim</font> <font color="#660000">sRoot</font> <font color="#000099">As String,</font> <font color="#660000">sDir</font> <font color="#000099">As String,</font> <font color="#660000">sFile</font> <font color="#000099">As String &#160; Dim</font> <font color="#660000">iCnt</font> <font color="#000099">As Long,</font> <font color="#660000">iIdx</font> <font color="#000099">As Long &#160; &#160; If Not</font> <font color="#660000">FolderExists(sSpec)</font> <font color="#000099">Then Exit Function</font> &#160; &#160; <font color="#006600">' Add trailing backslash if missing</font> &#160; <font color="#660000">sRoot = AddBackslash(sSpec) &#160; iCnt</font> = 2& <font color="#006600">'.' '..'</font> &#160; &#160; <font color="#000099">On Error Resume Next</font> <font color="#006600">' Ignore file errors</font> &#160; <font color="#660000">sFile = Dir$(sRoot & "*.*", vbNormal)</font> &#160; <font color="#000099">Do While</font> <font color="#660000">LenB(sFile) &#160; &#160; &#160;SetAttributes sRoot & sFile, vbNormal &#160; &#160; &#160;Kill sRoot & sFile &#160; &#160; &#160;sFile = Dir$</font> &#160; <font color="#000099">Loop</font> &#160; &#160; On Error GoTo</font> HandleIt <font color="#006600">' No error should occur in here</font> &#160; <font color="#000099">Do:</font> <font color="#660000">sDir = Dir$(sRoot & "*", vbDirectory)</font> &#160; &#160; &#160;<font color="#000099">For</font> <font color="#660000">iIdx</font> = 1& <font color="#000099">To</font> <font color="#660000">iCnt &#160; &#160; &#160; &#160; sDir = Dir$</font> <font color="#006600">'.' '..' ['fail']</font> &#160; &#160; &#160;<font color="#000099">Next &#160; &#160; &#160;If</font> <font color="#660000">LenB(sDir)</font> = 0& <font color="#000099">Then Exit Do &#160; &#160; &#160;If</font> <font color="#660000">KillFolder(sRoot & sDir & DIR_SEP)</font> <font color="#000099">Then &#160; &#160; &#160;<font color="#006600">' Sub-folder is now gone but Dir$ was reset &#160; &#160; &#160;' during recursive call so Do Dir$(..) again</font> &#160; &#160; &#160;Else:</font> <font color="#660000">iCnt = iCnt</font> + 1& &#160; &#160; &#160;<font color="#006600">' Kill folder failed (remnant files) so skip &#160; &#160; &#160;' this folder (iCnt + 1) to get the rest</font> &#160; &#160; &#160;<font color="#000099">End If &#160; Loop &#160; &#160; If</font> <font color="#660000">bJustEmptyDontRemove</font> = <font color="#000099">False Then</font> <font color="#660000">RmDir sRoot</font> <font color="#006600">' Errors here if remnants</font> HandleIt: &#160; <font color="#660000">KillFolder</font> = <font color="#000099">Not</font> <font color="#660000">FolderExists(sSpec)</font> <font color="#000099">End Function &#160; &#160;'----------------------------------------------------- &#160; &#160; </font> </nobr> </p> </tt>