Results 1 to 12 of 12

Thread: File Searching using Dir? confusing...please read and help!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37

    Smile

    Hi guys,

    I searched thru this BBS about searching thru subdirs and I didn't really understand the code that people wrote.

    I saw various examples by 'Aaron Young' and 'Kedaman', but I didn't understand exactly what went on in those code snippets and how to implement it in my program.

    Here's what my program is supposed to do and what I don't understand how to do:

    My program is going to search thru a Directory and it's child sub-directories looking for MP3 files. Once it finds an mp3 file, it will extract the ID3Tag (got that working thanks to Kedaman) and store it in the database.

    Now what I don't understand is how to make the program search thru directories and find the mp3 file, extract/store the ID3 tag, and then continue searching for other mp3s and then do the same thing...reapeating until it finds no more files.

    Again, I saw the examples but I think they showed only how to search for directories and subdirectories.

    Thanks.
    please help?

    [Edited by madd indian on 05-29-2000 at 06:10 PM]

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Cool This could help.

    Fresh and fully commented code:
    Code:
    Option Explicit
    
    Sub GetID3Tag(ByVal sFile As String)
        ' Your code goes here.
        ' sFile is the full MP3 pathname, for example C:\MySong.MP3
    End Sub
    
    Sub ScanMP3Files(ByVal sPath As String)
        Dim sFile As String
        
        ' First, we have to add a slash to the end of the path name if it's not already there.
        sPath = sPath & IIf(Right(sPath, 1) = "\", vbNullString, "\")
        ' The above line, in English:
        ' Does sPath end with a slash? Don't add anything. Doesn't it? Add a slash.
        
        sFile = Dir(sPath & "*.MP3") ' Start looking for MP3 files in the path sPath.
        
        Do Until sFile = vbNullString ' Keep going until you can't find anything new
            Call GetID3Tag(sFile) ' Use your code
            sFile = Dir ' Search for a new file
        Loop ' Go back to the beginning of the loop. (Do Until...)
        
        ' Add your code here. It will be executed when the search completes.
    End Sub
    Usage:
    Call ScanMP3Files("D:\Songs")
    This will search for MP3 files in D:\Songs and will execute the GetID3Tag subroutine for each one.

  3. #3
    Lively Member
    Join Date
    Oct 1999
    Posts
    66
    Try the DIR function. It is the easiest way without API Calls. Look in the help for more info.

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37
    Yonatan:

    Thanks a lot buddy. However I encountered a few problems with your code when I put it in my project.

    The error message is "Bad Record Number"

    Here's where it gives the problem

    Code:
    '// this function gets the ID3TAG and sends it to ID3splitTAG which will parse the tag
    Function ID3getTAG(sFile As String)
        Dim astring As String * 128
        ff = FreeFile
        Open sFile For Binary As ff
            Get ff, LOF(ff) - 127, astring ' problem over here according to VB
        Close ff
        ID3TAG = astring
        Call ID3splitTAG(ID3TAG, id3title, ID3artist, ID3album, id3year, Id3comment, id3genre)
        
    End Function
    
    Public Sub FindMP3(ByVal sPath As String)
    Dim sFile As String
    
    sPath = sPath & IIf(Right(sPath, 1) = "\", vbNullString, "\")
    
    sFile = Dir(sPath & "*.mp3")
    
    Do Until sFile = vbNullString
        Call ID3getTAG(sFile)
        sFile = Dir
    Loop
            
    End Sub

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Try this...

    If you want to include SubFolders in the Search, then try this code I've put together, it uses the API to make the Search Very fast:

    In a Module:
    Code:
    'Simple MP3 ID TAG Type
    Public Type MP3TYPE
        mp3Title As String
        mp3Artist As String
        mp3Album As String
        mp3Year As String
        mp3Comment As String
        mp3Genre As Long
        FilePath As String
    End Type
    
    'API Consts, Types and Functions
    Private Const MAX_PATH = 260
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    
    Private Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type
    
    Private 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
    
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    
    Private aFileList As Variant
    Private nFileCount As Long
    
    Public Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant
        'Initialize the Private File Array and Count, then call the Fast
        'File Recursive Function to populate the Array, then return it.
        nFileCount = 0
        aFileList = Array()
        Call RecurseFindFiles(sFolder, sPattern)
        FastFindFiles = aFileList
    End Function
    
    Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String)
        Dim tFD As WIN32_FIND_DATA
        Dim lFile As Long
        Dim bFound As Long
        Dim aSubs() As String
        Dim nSubs As Long
        Dim sFilename As String
        
        'Make sure the passed folder includes an ending "\"
        If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
        
        'Find the First File in the Specified Location
        lFile = FindFirstFile(sFolder & "*", tFD)
        bFound = lFile
        
        'Loop while a File is found
        While bFound
            'Get the Filename
            sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
            If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
                'If it's a Folder, add it to the Sub Folders Array
                If Left(sFilename, 1) <> "." Then
                    ReDim Preserve aSubs(nSubs)
                    aSubs(nSubs) = sFilename
                    nSubs = nSubs + 1
                End If
            Else
                'If it's a File, compare it to the Pattern for a Match
                If sFilename Like UCase(sPattern) Then
                    'If it matches, add it to the File Array
                    ReDim Preserve aFileList(nFileCount)
                    aFileList(nFileCount) = sFolder & sFilename
                    nFileCount = nFileCount + 1
                End If
            End If
            'Find the Next File, (if there is one).
            bFound = FindNextFile(lFile, tFD)
        Wend
        'Close the API Find Handle
        Call FindClose(lFile)
        
        'If there were Sub Folders found, Recurse them too..
        If nSubs Then
            For nSubs = 0 To UBound(aSubs)
                Call RecurseFindFiles(sFolder & aSubs(nSubs), sPattern)
            Next
        End If
    End Sub
    
    Public Function ID3getTAG(ByVal sFile As String) As MP3TYPE
        'Get ID Tag info from the End of an MP3 File
        Dim sBuff As String * 128
        Dim iFile As Integer
        
        iFile = FreeFile
        Open sFile For Binary As iFile
            Get #iFile, LOF(iFile) - 127, sBuff
        Close iFile
        
        'See if there is any TAG Info
        If Left(sBuff, 3) <> "TAG" Then Exit Function
        
        'If there is, seperate it out into our User Defined Type and Return it.
        With ID3getTAG
            .mp3Title = Mid(sBuff, 4, 30)
            .mp3Artist = Mid(sBuff, 34, 30)
            .mp3Year = Mid(sBuff, 94, 4)
            .mp3Comment = Mid(sBuff, 98, 30)
            .mp3Album = Mid(sBuff, 64, 30)
            .mp3Genre = Asc(Right(sBuff, 1))
            .FilePath = sFile
        End With
    End Function
    In a Form with a ListView and Command Button:
    Code:
    Private Sub Form_Load()
        'Create some Columns in the ListView
        With ListView1
            .View = lvwReport
            .ColumnHeaders.Add , , "Title"
            .ColumnHeaders.Add , , "Artist"
            .ColumnHeaders.Add , , "Album"
            .ColumnHeaders.Add , , "Genre"
            .ColumnHeaders.Add , , "Year"
            .ColumnHeaders.Add , , "Comment"
        End With
    End Sub
    
    Private Sub Command1_Click()
        Dim nCount As Long
        Dim aList As Variant
        Dim aMP3s() As MP3TYPE
        
        'Get a list of all MP3's in the specified Folder, (and SubFolders).
        aList = FastFindFiles("E:\MP3", "*.mp3")
        
        ReDim aMP3s(UBound(aList))
        
        'Get the ID Tags and Add the Details to the ListView
        ListView1.ListItems.Clear
        For nCount = 0 To UBound(aList)
            aMP3s(nCount) = ID3getTAG(aList(nCount))
            With ListView1.ListItems.Add(, , aMP3s(nCount).mp3Title)
                .SubItems(1) = aMP3s(nCount).mp3Artist
                .SubItems(2) = aMP3s(nCount).mp3Album
                .SubItems(3) = aMP3s(nCount).mp3Genre
                .SubItems(4) = aMP3s(nCount).mp3Year
                .SubItems(5) = aMP3s(nCount).mp3Comment
            End With
        Next
    End Sub

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37
    Aaron Young: THANK YOU SOO MUCH! Now I know why your called the 'Guru'!!!

    You basically did the whole project for me!! I actually wanted to do a little on my own! Well I'll add the database part on my own...hopefully.

    VB should make a function that searches for files thru directories! Make life so much more easier!

    Thanks again Aaron Young..I'll try the code tomorrow when I get a chance on the computer!

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37
    this is probably a dumb question but how would I add the ListView control on the form?

    Is it the same as the ListBox control?

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Select Components from the Project Menu and a list of all available components is shown, select Microsoft Windows Common Controls, check it, then press OK and the ListView control will be added to your Toolbar.

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37
    Mr. Aaron Young: Thanks for your code. It works, but only for a few songs...it didn't work when I selected my whole MP3 dir. That dir only has around 200 songs and a few subdirs. It worked however with a dir that only have around 7 songs.

    I think it may be that the array can't take any more information?

    The error was "Out of Stack Space"

  10. #10
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Not sure why that's happening, it works on my machine searching a Folder which has a dozen or so sub folders and a total of 292 MP3's, it also works if I tell it to seach from the root of the Drive.

  11. #11

    Thread Starter
    Member
    Join Date
    Jan 2000
    Posts
    37
    Damn....

    well thanks for your help...how can I tell in VB what part of the code it's going thru when I run it?

  12. #12
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Either Step through the Whole program using "F8" or set a Breakpoint in the Program and then when it Breaks at that point continue stepping through the Program using the "F8" key.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width