Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all files within a directory vb6

I was wondering if anyone could help me with a vb6 function that would delete all files within a directory (excluding subdirectories).

like image 263
zSynopsis Avatar asked Dec 06 '22 04:12

zSynopsis


2 Answers

One line, using the VB6 statement Kill

Kill "c:\doomed_dir\*.*"

The help topic says "In Microsoft Windows, Kill supports the use of multiple-character (*) and single-character (?) wildcards to specify multiple files".

As an aside - I prefer to avoid the Microsoft Scripting Runtime (including FileSystemObject). In my experience it's occasionally broken on user machines, perhaps because their IT department are paranoid about viruses.

like image 160
MarkJ Avatar answered Jan 07 '23 00:01

MarkJ


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
like image 28
Alex Avatar answered Jan 06 '23 23:01

Alex