?
Solved

Parsing a directory, including sub directory

Posted on 2004-10-15
4
Medium Priority
?
649 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
[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
4 Comments
 
LVL 1

Accepted Solution

by:
VBtorment earned 120 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 120 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 100 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

771 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