PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Using DIR function with multiple specific file types-VBForums
Results 1 to 33 of 33

Thread: [RESOLVED] Using DIR function with multiple specific file types

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Resolved [RESOLVED] Using DIR function with multiple specific file types

    Hi everyone,

    I've been all over the web and also experimenting on my own and cannot find an answer to this.

    One of my programs uses a hidden VB FileListBox for filename loading and extraction. Works fine except for the upper limit of 32767 items in the FileListBox. I've been experimenting with using the DIR function in order to eliminate the hidden FileListBox and to streamline code. So far so good.

    But here's the problem - I can find no way to use multiple specific file types - i.e. multiple wildcard patterns such as "*.bmp;*.jpg;*.txt" etc.

    If using just one file type then all is well but not multiple types. Here is my code (Hope I post it correctly, its my first time):

    Code:
    1. Private Sub ListFiles(strPath As String, Optional Extension As String)
    2.    
    3.     '- This is a good alternative to using a hidden FileListBox to list files.
    4.     '- BUT There is (as always) a caveat. In this case it is that multiple
    5.     '- file patterns are not accepted.
    6.    
    7.     Dim File As String
    8.     Dim fCount As Long
    9.     Dim p As Long
    10.    
    11.     Erase strFiles
    12.    
    13.    
    14.     If Right$(strPath, 1) <> "" Then strPath = strPath & ""
    15.    
    16.     If Trim$(Extension) = "" Then
    17.         Extension = "*.*"
    18.     ElseIf Left$(Extension, 2) <> "*." Then
    19.         Extension = "*." & Extension
    20.     End If
    21.    
    22.     File = Dir$(strPath & Extension)
    23.    
    24.     Do While Len(File)
    25.         fCount = fCount + 1
    26.         ReDim Preserve strFiles(fCount)
    27.         strFiles(fCount) = File
    28.         File = Dir$
    29.     Loop
    30.    '  .....Etc.
    31.    
    32.    
    33.    
    34. End Sub


    Any help is appreciated, thank you

  2. #2
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,457

    Re: Using DIR function with multiple specific file types

    How many wild card patterns do you think you may need to match - a reasonable number (few) or hundreds +? If just a few, create a string array with a list of them and then add another loop to your code to execute it for each successive wild card value.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    Only 8-10 file types for now but maybe more later.

    I thought about doing as you suggest, but it might raise a performance hit. But, it may also be the only way......

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    Oops, hit post button twice

  5. #5
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,560

    Re: Using DIR function with multiple specific file types

    Quote Originally Posted by SomeYguy View Post
    I thought about doing as you suggest, but it might raise a performance hit.
    The bottleneck when enumerating files/folders is *always* disk access. When enumerating *.bmp wilcard for instance, the OS has to read the contents of the whole directory so this is *not* faster than enumerating everything w/ *.*

    In your case I would consider full enumeration from OS (i.e. *.* + vbDirectory) and then filter "client-side" w/ VB's Like operator in a loop. It's not worth it performance-wise to prepare a single regexp from an array of mask wilcards -- something like /pattern1|pattern2|pattern2|.../ -- as this is error prone and you have to test very well file name escapes not to leak control symbols.

    cheers,
    </wqw

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,557

    Re: Using DIR function with multiple specific file types

    There is also the option of a query against the Windows Search database.

  7. #7
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,560

    Re: Using DIR function with multiple specific file types

    Yes, and if 32k entries limit is expected to be reached then OP might try a direct MFT parsing approach on an NTFS volume too (although its greatest strength shows for recursive enumerations).

    cheers,
    </wqw>

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,328

    Re: Using DIR function with multiple specific file types

    The PathMatchSpec API supports multiple filters, passed as semicolon delimited e.g. *.jpg;*.gif;*.bmp etc., I think that's a much better option than the Like statement when you're going beyond a single pattern.

    Code:
    Private Declare Function PathMatchSpecW Lib "shlwapi" (ByVal pszFileParam As Long, ByVal pszSpec As Long) As Long
    
    Private Function ShowFile(sName As String, sFilter As String) As Boolean
    If PathMatchSpecW(StrPtr(sName), StrPtr(sFilter)) Then
        ShowFile = True
    Else
        ShowFile = False
    End If
    End Function
    Note you pass just the filename, not the full path. The code above supports Unicode.

  9. #9
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,560

    Re: Using DIR function with multiple specific file types

    PathMatchSpecW seems like a perfect fit from the wonderful world of shlwapi functions :-))

    The performance of Like operator is not that awful for the API to be much better but it's the # symbol and [a-z] class matches that differ to rudimentary filesystem wildcards and this might be a problem in some edge-cases.

    Thumbs up! (I think I could use this API in some of my (public) classes too, provided that it's impl since win2k)

    cheers,
    </wqw>

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,123

    Re: Using DIR function with multiple specific file types

    A 'simpler' option MAY be (depending upon your program), multiple FileListBoxes. Perhaps one for each of the different file types.

    Just a thought, seeing as how your ONE FLB works fine, but is limited to the 32767.

  11. #11

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,929

    Re: Using DIR function with multiple specific file types

    Over 32k files in a folder seems more than a bit excessive to me. Why so many files in a single folder?
    Perhaps creating some sub directories and copying the files of the given types to their own child folder may be a solution.

  13. #13
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,711

    Re: Using DIR function with multiple specific file types

    It looks like you're getting good advice, but here's some code where I do something similar. Take a look at the LoadMovieFiles procedure. It loads a listbox with certain movie files it finds.

    Code:
    
    Public Sub LoadMovieFiles(lst As ListBox, sPathToExamine As String)
        Dim sFle As String
        '
        sFle = Dir$(AddSlash(sPathToExamine) & "*.*")
        Do
            If Len(sFle) = 0 Then Exit Do
            If FileIsVideo(sFle) Then
                lst.AddItem sFle
            End If
            sFle = Dir$
        Loop
    End Sub
    
    Public Function AddSlash(sFldr As String) As String
        ' Only adds it if not there.
        ' This can be used as EITHER a sub or function, as it modifies the input.
        If Right$(sFldr, 1) <> "\" Then sFldr = sFldr & "\"
        AddSlash = sFldr
    End Function
    
    Public Function FileIsVideo(sFile As String) As Boolean
        FileIsVideo = (LCase$(Left$(Right$(sFile, 3), 2)) = "mp") Or _
                      (LCase$(Left$(Right$(sFile, 4), 2)) = "mp") Or _
                      (LCase$(Right$(sFile, 3)) = "avi") Or _
                      (LCase$(Right$(sFile, 3)) = "wmv")
    End Function
    
    

    Maybe those ideas will help you to get going.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    Quote Originally Posted by DataMiser View Post
    Over 32k files in a folder seems more than a bit excessive to me. Why so many files in a single folder?
    Perhaps creating some sub directories and copying the files of the given types to their own child folder may be a solution.
    I agree. But this program will deal primarily with image files, and some folks just keep dumping their digital images into the same folder for years n years . I'd like to extend the upper limit to ~ 100k or so. Also, I'm streamlining and so using a hidden FileListBox just bugs me .

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    Unrelated: Not sure why but when I reply I get a wait 30 seconds warning even though I haven't yet submitted and then the post shows up twice?
    Last edited by SomeYguy; Jun 14th, 2019 at 05:55 PM.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,123

    Re: Using DIR function with multiple specific file types

    Just sent note to MOD about your 'wait' issue

  17. #17
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,218

    Re: Using DIR function with multiple specific file types

    I have noticed the double posting for a couple of weeks now.
    If you go into advanced edit(creation), it does not happen.
    If you do what you have been doing (not advanced) it actually does send it on your first attempt.
    If you close the Tab, and go back into the thread via the Forum list of the various threads, you will find that your post is in there.

  18. #18
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,456

    Re: Using DIR function with multiple specific file types

    Quote Originally Posted by SomeYguy View Post
    Unrelated: Not sure why but when I reply I get a wait 30 seconds warning even though I haven't yet submitted and then the post shows up twice?
    It's a bug with the forum software... showed up after an upgrade a while back. No one's sure what's causing it or how to fix it. Has something to do with one of the extesions.... BUT if you look, after getting the wait message, and scroll down... you'll see that your post DID in fact post... and that's why you're getting the wait message.... the wait is a part of the anti-spam feature that forces posters to wait a certain amount of time between posts. In this case, there's something that causes the post to go through the first time, and then again a second time, which then gets caught, resulting in the wait message.... and of course, people aren't paying attention, wait, and post it again, and results in the double post.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,929

    Re: Using DIR function with multiple specific file types

    Quote Originally Posted by SomeYguy View Post
    Unrelated: Not sure why but when I reply I get a wait 30 seconds warning even though I haven't yet submitted and then the post shows up twice?
    That happens to me all the time but it is after I hit post reply the first time I get the message and the screen looks like it has not been posted. If I wait and hit the post button then it is a double post. If I simply go back to the thread I can see that the message was posted and never duplicated.

  20. #20
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    402

    Re: Using DIR function with multiple specific file types

    The double post issue is known and discussed in the Forum Feedback.

  21. #21
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,100

    Re: Using DIR function with multiple specific file types

    Also, some folks have suggested that this ONLY happens in the quick reply, so if you hit Go Advanced, and use that to submit messages, then perhaps it won't happen. I haven't tested it out, myself, very well. In fact, this will be my first test.
    My usual boring signature: Nothing

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    Very good on the double post issue and thanks. I'm using Advanced now rather than quick so here goes .

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    YAY!! Multiple file pattern w/Dir issue is resolved! Per cool little API code from Fafalone, I used the PathMatchSpecW API function. Works well.

    I shoehorned it all into one sub, and here is the code in case anybody else can use.


    Code Code:
    1. Private Declare Function PathMatchSpecW Lib "shlwapi" (ByVal pszFileParam As Long, ByVal pszSpec As Long) As Long
    2.  
    3.  
    4. Private Sub ListFiles(strPath As String, Optional strFilePattern As String)
    5.    
    6.     '- This is a good alternative to using a hidden FileListBox to list files.
    7.     '- Note the use of Dir$ rather than Dir for that function. I read somewhere
    8.     '- on an MVP site that Dir$ is marginally faster.
    9.    
    10.     Dim File              As String
    11.     Dim fCount          As Long
    12.     Dim nX               As Long
    13.    
    14.     Dim strFiles()      As String
    15.     '- Define file pattern
    16.     If strFilePattern = "" then strFilePattern = "*.*"
    17.     '- Fix path if needed.
    18.     If Right$(strPath, 1) [noparse]<> "\" Then strPath = strPath & "\"[/noparse]
    19.     '- We want to see all files before we filter
    20.     File = Dir$(strPath & "*.*")
    21.     '- Do it
    22.     Do While Len(File) > 0
    23.         fCount = fCount + 1
    24.         ReDim Preserve strFiles(fCount)
    25.         strFiles(fCount) = File
    26.         '- Pattern filter
    27.         If PathMatchSpecW(StrPtr(File), StrPtr(strFilePattern)) Then
    28.        
    29.         '- ** This is where you do something with the returned file names.
    30.         '- Can add to a listbox, string array, or ? **
    31.        
    32.         '- Example: List1.Additem File
    33.         End If
    34.         File = Dir$
    35.     Loop
    36.    
    37. End Sub

    EDIT: Code updated to include suggestions.

    Thanks again!
    Last edited by SomeYguy; Jun 16th, 2019 at 02:23 PM.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: Using DIR function with multiple specific file types

    .....
    Last edited by SomeYguy; Jun 15th, 2019 at 11:39 AM.

  25. #25
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,328

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Right(File, Len(File) - InStrRev(File, ""))

    That will return a blank string, it should be Right(File, Len(File) - InStrRev(File, "\"))

    **I presume that's what you actually posted but it seems the forum code converts "\" into "" for some reason (I used the NOPARSE tag to get it to show)

  26. #26
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,394

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by fafalone View Post
    Right(File, Len(File) - InStrRev(File, ""))

    That will return a blank string, it should be Right(File, Len(File) - InStrRev(File, "\"))

    **I presume that's what you actually posted but it seems the forum code converts "\" into "" for some reason (I used the NOPARSE tag to get it to show)
    Actually, is that check needed at all. Doesn't Dir$() return just the file name? Also, looks like not specifically handling Dir$() when it returns "." or ".."; though may not be important in that scenario? Looks like the 1st instance is handled by calling Dir$() before the loop.

    Edited: ignore that last part as it applies when enumerating directories, not the case here

    Though "File = Dir$" line should be moved to just before the "Loop" statement after "End If". As it is right now, the 1st file in the directory is never being checked.
    Last edited by LaVolpe; Jun 15th, 2019 at 03:11 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  27. #27

  28. #28
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,394

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by fafalone View Post
    Oops yeah Dir just returns the filename, been a looooooong time since I enumerated files without some fancy way lol
    Me too. Can't remember offhand what scenarios . or .. is returned in the enumeration
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  29. #29

  30. #30
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,394

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by fafalone View Post
    I ran that code on a folder 3 levels deep printing all names, didn't get . or .. in the list.
    Ugh, now you made me go and find the scenario... it's when you include vbDirectory in the Dir$() command
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  31. #31

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by fafalone View Post
    Right(File, Len(File) - InStrRev(File, ""))

    That will return a blank string, it should be Right(File, Len(File) - InStrRev(File, "\"))

    **I presume that's what you actually posted but it seems the forum code converts "\" into "" for some reason (I used the NOPARSE tag to get it to show)
    Ugh. You're right, forum code took out my backslashes. I edited using the noparse tags and backslashes are showing now but so are the noparse tags .

  32. #32

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    24

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by LaVolpe View Post
    Actually, is that check needed at all. Doesn't Dir$() return just the file name? Also, looks like not specifically handling Dir$() when it returns "." or ".."; though may not be important in that scenario? Looks like the 1st instance is handled by calling Dir$() before the loop.

    Edited: ignore that last part as it applies when enumerating directories, not the case here

    Though "File = Dir$" line should be moved to just before the "Loop" statement after "End If". As it is right now, the 1st file in the directory is never being checked.
    Great catches and thanks . In this scenario, I only need the file names within the current directory.

    EDIT: Thanks especially for catching that "File = Dir$" line placement error. That cured a little issue which was driving me nuts!
    Last edited by SomeYguy; Jun 15th, 2019 at 04:35 PM.

  33. #33
    Frenzied Member
    Join Date
    Dec 2008
    Posts
    1,218

    Re: [RESOLVED] Using DIR function with multiple specific file types

    Quote Originally Posted by SomeYguy View Post
    Ugh. You're right, forum code took out my backslashes. I edited using the noparse tags and backslashes are showing now but so are the noparse tags .
    If there is any chance that the posted code is confused(mangled), perhaps attach a small project

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width