by Gary Choma (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (2 Votes)
This function attempts to delete all files
and subdirectories of the given
directory name, and leaves the given
directory intact, but completely empty.
If the Kill command generates an error (i.e.
file is in use by another process -
permission denied error), then that file and
subdirectory will be skipped, and the
program will continue (On Error Resume Next).
EXAMPLE CALL:
ClearDirectory "C:\Temp\"
Inputs
Full path directory name
Assumes
Kill statement may error out for various reasons, which will prevent those files/directories from being deleted.
Code Returns
N/A
Side Effects
WARNING: If a subdirectory is prevented from being deleted (i.e. Kill statement errors out because of file access error), the loop will NOT terminate (While Len(sSubDir) > 0).
This may not be an issue in most cases, but I just wanted to make clear the limitations of this code.
Private Sub ClearDirectory(psDirName)
'This function attempts to delete all files
'and subdirectories of the given
'directory name, and leaves the given
'directory intact, but completely empty.
'
'If the Kill command generates an error (i.e.
'file is in use by another process -
'permission denied error), then that file and
'subdirectory will be skipped, and the
'program will continue (On Error Resume Next).
'
'EXAMPLE CALL:
' ClearDirectory "C:\Temp\"
Dim sSubDir
If Len(psDirName) > 0 Then
If Right(psDirName, 1) <> "\" Then
psDirName = psDirName & "\"
End If
'Attempt to remove any files in directory
'with one command (if error, we'll
'attempt to delete the files one at a
'time later in the loop):
On Error Resume Next
Kill psDirName & "*.*"
DoEvents
sSubDir = Dir(psDirName, vbDirectory)
Do While Len(sSubDir) > 0
'Ignore the current directory and the
'encompassing directory:
If sSubDir <> "." And _
sSubDir <> ".." Then
'Use bitwise comparison to make
'sure MyName is a directory:
If (GetAttr(psDirName & sSubDir) And _
vbDirectory) = vbDirectory Then
'Use recursion to clear files
'from subdir:
ClearDirectory psDirName & _
sSubDir & "\"
'Remove directory once files
'have been cleared (deleted)
'from it:
RmDir psDirName & sSubDir
DoEvents
'ReInitialize Dir Command
'after using recursion:
sSubDir = Dir(psDirName, vbDirectory)
Else
'This file is remaining because
'most likely, the Kill statement
'before this loop errored out
'when attempting to delete all
'the files at once in this
'directory. This attempt to
'delete a single file by itself
'may work because another
'(locked) file within this same
'directory may have prevented
'(non-locked) files from being
'deleted:
Kill psDirName & sSubDir
sSubDir = Dir
End If
Else
sSubDir = Dir
End If
Loop
End If
End Sub