Delete all files in the vb6 directory

I was wondering if anyone could help me with the vb6 function, which will delete all the files in the directory (excluding subdirectories).

+4
source share
4 answers

One line using the VB6 kill operator

Kill "c:\doomed_dir\*.*" 

In the help section. On Microsoft Windows, Kill supports the use of multi-character (*) and single-character (?) Wildcard characters to indicate multiple files. "

Aside - I prefer to avoid Microsoft Scripting Runtime Runtime (including FileSystemObject). In my experience, it sometimes crashed on user machines, perhaps because their IT department was paranoid about viruses.

+7
source

I believe this should work:

 Dim oFs As New FileSystemObject Dim oFolder As Folder Dim oFile As File If oFs.FolderExists(FolderSpec) Then Set oFolder = oFs.GetFolder(FolderSpec) 'caution! On Error Resume Next For Each oFile In oFolder.Files oFile.Delete True 'setting force to true will delete a read-only file Next DeleteAllFiles = oFolder.Files.Count = 0 End If End Function 
+4
source

I have not tested every scenario, but it should work. It should delete every file, and if the file is locked or you do not have access, you should get Error 70, which is caught, and you will get the Abort, Retry or Ignore field.

 Sub DeleteAllFilesInDir(ByVal pathName As String) On Error GoTo errorHandler Dim fileName As String If Len(pathName) > 0 Then If Right(pathName, 1) <> "\" Then pathName = pathName & "\" End If fileName = Dir(pathName & "*") While Len(fileName) > 0 Kill pathName & fileName fileName = Dir() Wend Exit Sub errorHandler: If Err.Number = 70 Then Select Case MsgBox("Could not delete " & fileName & ". Permission denied. File may be open by another user or otherwise locked.", vbAbortRetryIgnore, "Unable to Delete File") Case vbAbort: Exit Sub Case vbIgnore: Resume Next Case vbRetry: Resume End Select Else MsgBox "Error deleting file " & fileName & ".", vbOKOnly Or vbCritical, "Error Deleting File" End If End Sub 
+2
source

It would seem that the FileSystemObject DeleteFile method for Scripting scripts also supports wildcards, as this works for me:

 Dim fs As New Scripting.FileSystemObject fs.Deletefile "C:\Temp\*.jpg", true 

This approach has less control than the approach proposed by @Corazu, but may have some utility in certain cases.

+1
source

All Articles