Solved

Parsing a directory, including sub directory

Posted on 2004-10-15
4
621 Views
Last Modified: 2013-11-18
I'm using VB6, and decided to start thinking about a small utility project.  (I prefer to write my own small stuff, as I can usually get it faster than searching for something online.)

This project needs the ability to look through a given directory (folder, whatever), check for either a specific file or type of file (*.mp3, for example), and parse its way through any and all sub folders.  I admit that I haven't looked for the info much, as I have only just begun to plan the project.

Any ideas, including pseudo code would be helpfull.

Thanks
0
Comment
Question by:Mixael
4 Comments
 
LVL 1

Accepted Solution

by:
VBtorment earned 30 total points
ID: 12319421
Hai, Mixael

Public Const MAX_PATH = 260
   
Public Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
 
Public Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type

Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Public FCount As Long


Sub LoopFolders(sPath$)
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim cont As Integer
Dim sFile As String

    cont = True
    sPath$ = bsl(sPath$)
    'find the first file matching the parameter \*.*
    hFile = FindFirstFile(sPath & "*.*" & Chr$(0), WFD)
    If hFile <> -1 Then
        While cont
            sFile = TrimNull(WFD.cFileName)
             'ignore the 2 standard root entries
            If (sFile <> ".") And (sFile <> "..") Then
                If (WFD.dwFileAttributes And vbDirectory) Then
                    FCount = FCount + 1
                    'Do your stuf here for dirs
                    'LoopFolders sPath & sFile 'recurse
                Else    'bestanden
                    'Do your stuf here for Files
                End If
            End If
            cont = FindNextFile(hFile, WFD)
        Wend
    End If
    Call FindClose(hFile)
End Sub


Public Function bsl(Path$) As String
    If Right$(Path$, 1) = "\" Then
        bsl = Path$
    Else
        bsl = Path$ & "\"
    End If
End Function

0
 
LVL 8

Assisted Solution

by:bramsquad
bramsquad earned 30 total points
ID: 12319529
here are some self explanitory function that should help you out

Function GetPath(FullPath As String) As String
    GetPath = Left(FullPath, Len(FullPath) - (InStr(1, StrReverse(FullPath), "\") - 1))
End Function

Function GetExtension(FullPath As String) As String
    GetExtension = Right(FullPath, Len(FullPath) - (InStr(1, (FullPath), ".") - 1))
End Function

Function GetFileName(FullPath As String) As String
    GetFileName = Left(Right(FullPath, Len(FullPath) - (InStr(1, StrReverse(FullPath), ".") - 1)), Len(Right(FullPath, Len(FullPath) - (InStr(1, StrReverse(FullPath), ".") - 1))) - Len(Right(FullPath, Len(FullPath) - (InStr(1, (FullPath), ".") - 1))))
End Function

Function FileExists(FullPath As String) As Boolean
    If Dir(FullPath) = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function

~b
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 25 total points
ID: 12320000
Here is another way to do it using the built in methods from VB (the API method presented by VBtorment is the fastest).  I though I would present this method because it shows how to add the matches to collection.  Create a new project and add a CommandButton, Label and a TextBox.  For the TextBox, set the MultiLine property to True and the ScrollBars property to 3 - Both.

Option Explicit

Private matches As Collection

Private Sub Form_Load()
    Label1.Caption = ""
End Sub

Private Sub Command1_Click()
    Command1.Enabled = False

    Dim match As Variant
   
    Set matches = New Collection
    findFiles "C:\Documents and Settings\Michael\My Documents\", "*.txt"
    Text1.Text = ""
    For Each match In matches
        Text1.Text = Text1.Text & match & vbCrLf
    Next
    Label1.Caption = matches.Count & " file(s) found"
   
    Command1.Enabled = True
End Sub

Private Sub findFiles(ByVal basePath As String, ByVal filter As String)
    Dim curFile As String
    Dim subdir As Variant
    Dim subDirs As Collection
   
    ' add matching files in current directory
    curFile = Dir(basePath & filter)
    Do While curFile <> ""
        Label1.Caption = basePath & curFile
        matches.Add basePath & curFile
        DoEvents
        curFile = Dir()
    Loop
   
    ' build subDirs collection
    Set subDirs = New Collection
    curFile = Dir(basePath, vbDirectory)
    Do
        If curFile <> "" Then
            If (GetAttr(basePath & curFile) And vbDirectory) = vbDirectory Then
                If curFile <> "." And curFile <> ".." Then
                    subDirs.Add basePath & curFile & "\"
                End If
            End If
        End If
        DoEvents
        curFile = Dir()
    Loop While curFile <> ""
   
    ' recurse into each subdir
    For Each subdir In subDirs
        findFiles subdir, filter
    Next
End Sub

0
 

Author Comment

by:Mixael
ID: 12320072
All three good answers, and have me pointed in the right direction.  Although I haven't tried any of these answers yet, I shall do so this weekend.  I felt that all three answers were on the mark, and split the points the way I did based only on the order the answers came in.  (I should have increased the point value first, but got ahead of myself.)  To be honest, I didn't expect any answers so soon!

Thanks for the answers.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to count occurrences of each item in an array.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now