• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

Traversing folders and subfolders with DirListBox and FileListBox

Hi,

I want to select a folder and check for files and delete them if found. I don't want to just kill everything, only files in the FileListBox mask. (ie *.tmp)

I just can't get it.

Here's what I have so far. (VB6)

Dim strDir As String
Dim cnt As Integer
cnt = Dir1.ListCount
For i = -1 To cnt
    strDir = Dir1.List(i)
    Dir1 = strDir ' resynch the dirlistbox and filelistbox
    For j = 0 To (File1.ListCount - 1)
        xDumb = Strip(strDir, File1.List(j))
    Next j
Next i

It seems that I can get into the first subfolder then I crash and burn.

0
StevenJames
Asked:
StevenJames
1 Solution
 
robertleesCommented:
Here is what I would do. Somehow you find the folder to be processed. When you call DeleteFiles passing the name of this folder, this procedure will build one array of folders and subfolders, then examine each in turn. In each folder, it will add to a second array just the files with a 'tmp' extension - of course you could put any sort of test here for the files to be deleted. Finally it will pass through the second array killing all those files.

Sub DeleteFiles(ByVal strDirectory As String)
    Dim aryDirectories() As String
    Dim aryFiles() As String
    Dim d As Integer
    Dim n As Integer
    Dim strFile As String
   
    d = 1
    ReDim aryDirectories(d)
    aryDirectories(d) = strDirectory
   
    ReDim aryFiles(0)
   
    Do
        If d > UBound(aryDirectories) Then Exit Do
        strDirectory = aryDirectories(d)
        d = d + 1
        If Right(strDirectory, 1) <> "\" Then
            strDirectory = strDirectory & "\"
        End If
       
        strFile = Dir(strDirectory & "*.*", 31)
        Do Until strFile = ""
            If Left(strFile, 1) <> "." Then
                If GetAttr(strDirectory & strFile) = vbDirectory Then
                    n = UBound(aryDirectories) + 1
                    ReDim Preserve aryDirectories(n)
                    aryDirectories(n) = strDirectory & strFile
                Else
                    If LCase(Right(strFile, 4)) = ".tmp" Then
                        n = UBound(aryFiles) + 1
                        ReDim Preserve aryFiles(n)
                        aryFiles(n) = strDirectory & strFile
                    End If
                End If
            End If
            strFile = Dir
        Loop
    Loop
   
    For n = 1 To UBound(aryFiles)
        Kill aryFiles(n)
    Next n
    Stop
End Sub
0
 
robertleesCommented:
Oops. just realised that I left a STOP near the end of the code I sent earlier.
0
 
JonyvCommented:
Here is a slightly shorter solution using the FileSystemObject, just call "RecursiveDelete" with your topfolder and the filemask.
The function will delete files even if they are readonly, to change this, just remove ", True" from the FS.DeleteFile call

Private FS As Object

Private Sub Form_Load()

Set FS = CreateObject("Scripting.FileSystemObject")

End Sub


Private Sub RecursiveDelete(StartingFolder As String, FileMask As String)

Dim SubFolder As Object
Dim FoundFile As Object

If Not FS.FolderExists(StartingFolder) Then Exit Sub

For Each SubFolder In FS.GetFolder(StartingFolder).SubFolders
    RecursiveDelete SubFolder.Path, FileMask
Next

For Each FoundFile In FS.GetFolder(StartingFolder).Files
    If FoundFile.Name Like FileMask Then FS.DeleteFile FoundFile.Path, True
Next

End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
StevenJamesAuthor Commented:
Both work great! But I really want to use the arrays provided by the DirListBox and DirListBox. Isn't this what they're for? Isn't the point of these object to save coding and simplify everything?
0
 
StevenJamesAuthor Commented:
Both work great! But I really want to use the arrays provided by the DirListBox and DirListBox. Isn't this what they're for? Isn't the point of these object to save coding and simplify everything?
0
 
robertleesCommented:
The DirListBox and FileListBox are OK for interactive examination of your file structure, but don't allow exploration of nested directories - at least not easily. You would still have to somehow iterate through the DirListBox, selecting each directory in turn and processing its FileListBox, then look for sub-directories in each directory, and so on. You have no need of the user interface of these controls, so the code I sent earlier does the same job without having to be constrained by the needs of these controls.
0
 
CleanupPingCommented:
StevenJames:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
GPrentice00Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

 -->PAQ - no points refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER

GPrentice00
Cleanup Volunteer
0
 
YensidModCommented:
This question is PAQed and no points refunded (of 20)

YensidMod
Expert Exchange Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now