Visual Basic > Files Directories Drives
Delete Multiple Files
Delete Multiple Files To delete multiple files from a directory based on a string pattern eg. "C:\temp\*.tmp", use the following routines: 'Purpose : Delete single or multiple files, based on the sFileName criteria 'Inputs : sFileName The full path and name of a file (can include either * or $) ' [bDeleteReadOnly] If True, routine will delete readonly files 'Outputs : Returns True if errors where encountered 'Notes : VBA.Kill will stop deleting and raise an error, as soon as it fails to delete a file. ' This routine will continue trying to delete any matching files.'Revisions : Function FileKill(sFileName As String, Optional bDeleteReadOnly As Boolean) As Boolean Dim sThisFile As String, sThisPath As String On Error Resume Next If InStr(1, sFileName, "*") > 0 Or InStr(1, sFileName, "$") > 0 Then 'Kill multiple files sThisPath = PathFileToPath(sFileName) sThisFile = Dir(sFileName, vbNormal) Do While Len(sThisFile) If bDeleteReadOnly Then SetAttr sThisPath & sThisFile, vbNormal End If Kill sThisPath & sThisFile sThisFile = Dir Loop Else If bDeleteReadOnly Then SetAttr sFileName, vbNormal End If Kill sFileName End If FileKill = (Err.Number <> 0) On Error GoTo 0 End Function 'Purpose : Strips off the file name from a string. 'Inputs : sPathFileName A path and file name 'Outputs : The path the file is stored in. ' eg. "C:\WinNT\System32\Xcopy.exe" returns "C:\WinNT\System32\" 'Notes : In VB/Excel 2000, alter function to use InStrRev for speed 'Revisions : Function PathFileToPath(sPathFileName As String) As String Dim lThisChar As Long For lThisChar = 0 To Len(sPathFileName) - 1 If Mid$(sPathFileName, Len(sPathFileName) - lThisChar, 1) = "\" Then PathFileToPath = Left$(sPathFileName, Len(sPathFileName) - lThisChar) Exit For End If Next End Function
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows