?
Solved

Traversing folders and subfolders with DirListBox and FileListBox

Posted on 2003-03-26
9
Medium Priority
?
803 Views
Last Modified: 2011-10-03
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
Comment
Question by:StevenJames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:robertlees
ID: 8216011
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
 
LVL 1

Expert Comment

by:robertlees
ID: 8216092
Oops. just realised that I left a STOP near the end of the code I sent earlier.
0
 
LVL 3

Expert Comment

by:Jonyv
ID: 8216279
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
Industry Leaders: 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!

 
LVL 4

Author Comment

by:StevenJames
ID: 8217785
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
 
LVL 4

Author Comment

by:StevenJames
ID: 8218155
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
 
LVL 1

Expert Comment

by:robertlees
ID: 8220169
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
 

Expert Comment

by:CleanupPing
ID: 8899865
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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9440952
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
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9495733
This question is PAQed and no points refunded (of 20)

YensidMod
Expert Exchange Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question