Search Tools Links Login

Directory Cleaner (recursively)


Visual Basic 6, or VB Classic

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"

Original Author: Gary Choma

Inputs

Full path directory name

Assumptions

Kill statement may error out for various reasons, which will prevent those files/directories from being deleted.

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.

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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 120 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.