dcsimg
Results 1 to 11 of 11

Thread: Iterate dir and txt

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,782

    Iterate dir and txt

    C:\MYDIR\
    ...2015
    a.txt
    b.txt
    ...
    ...2016
    k.txt
    j.txt
    ...
    ...2017
    f.txt
    z.txt
    ....

    I need to loop in MYDIR, all subs dir(are year named), and for each year dir i need to loop "one to one" all internal files (all files are .txt file)...
    how to?

  2. #2
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,215

    Re: Iterate dir and txt

    Have you checked the help for the Dir() method?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,782

    Re: Iterate dir and txt

    Quote Originally Posted by Arnoutdv View Post
    Have you checked the help for the Dir() method?
    i dont love dir() method...:-(

  4. #4
    PowerPoster
    Join Date
    Oct 2013
    Posts
    3,215

    Re: Iterate dir and txt

    Uh??
    And what you are going to do about it?

    If you check the help you will see it's not that difficult

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    1,782

    Re: Iterate dir and txt

    Quote Originally Posted by Arnoutdv View Post
    Uh??
    And what you are going to do about it?

    If you check the help you will see it's not that difficult
    fso object

  6. #6
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,372

    Re: Iterate dir and txt

    Quote Originally Posted by Arnoutdv View Post
    Have you checked the help for the Dir() method?
    Hi Arnoutdv,

    I think that he thinks your the Help-CD

    come on Luca, you have been here since 2005 with over 1600 post's
    I get the impression that you are just a bit lazy

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    637

    Re: Iterate dir and txt

    The easiest way is to use the Dir$() function but there's a huge "Gotcha" with it - while you are looping through one directory using Dir$(), you cannot then loop through another directory - Dir$() "loses track" of where it is.

    So, I would suggest using Dir() to collect the year-level directories and then loop through each of those and use Dir() to loop through the individual files:

    Code:
    Dim sBaseDir as String : sBaseDir = "C:\MyDir" 
    Dim sQueue as String : sQueue = "" 
    Dim sDir as String 
    
    sDir = Dir$( sBaseDir  &"\*", vbDirectory )
    Do While ( "" <> sDir )
       sQueue = sQueue & IIf( "" <> sQueue, "|", "" ) & sDir 
       sDir = Dir$()
    Loop
    
    For Each eDir in Split( sQueue, "|" ) 
       sDir = Dir$( sBaseDir & "\" & eDir  & "*.txt", vbNormal )
       Do While ( "" <> sDir ) 
          ' Do Something with the file 
          sDir = Dir$() 
       Loop
    Next
    Regards, Phill W.

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

    Re: Iterate dir and txt

    I hate the Dir() function too. But FSO? You're killing me man, that's even worse, and god forbid the Shell32 object

    So, here's a bunch of other ways to enumeraute directories:

    You can use INamespaceWalk

    You can use FindFirstFile/FindNextFile

    You can use NtQueryDirectoryFile (very low level, the fastest, and hardest to use)

    Finally, you know I've never written up my standard directory enumeration to run automatically as a search? Hm. Basically you'd use SHCreateItemFromParsingName or SHCreateKnownFolderItem to get an IShellItem for the root, then pass off to a recursive function to enumerate it (bindtohandler/enum), list the items, and recurse on the folders (getattributes and sfgao_folder).

  9. #9
    Frenzied Member gibra's Avatar
    Join Date
    Oct 2009
    Location
    ITALY
    Posts
    1,582

    Re: Iterate dir and txt

    You can find a sample project that compare Dir$, API and FSO methods

    FindFile 3 methods: API, DIR, FSO
    http://nuke.vbcorner.net/Projects/VB...S/Default.aspx



    EDIT:
    if you installed MSDN Library 6.0 there is a project sample WINSEEK.vbp in folder:
    C:\Program Files (x86)\Microsoft Visual Studio\MSDN98\98VS\1040\SAMPLES\VB98\Filects

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,500

    Re: Iterate dir and txt

    Nevermind, that there is a more or less ready-to-use example of me in the Office-Forum (maybe some 2-3 months old), where i use FindFirst (and sisters) to collect files in a Folder-Tree
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    4,684

    Re: Iterate dir and txt

    A little late checking in here, but I often use a combination of Dir$() and a Collection to do these things. I'll agree that Dir$() is a bit dangerous that it's basically a project-global operation, and might be reset by some downstream function. However, if we just spin through it all, throwing everything into a Collection, then it's quite safe.

    Code:
    
        Dim c As New Collection
        Dim s As String
        Dim v As Variant
    
        s = Dir$("C:\Users\Elroy\Desktop\*.*")
        Do While Len(s)
            c.Add s, s
            s = Dir$
        Loop
        ' And now we've got our directory list in our c Collection and we can do what we want.
    
        For Each v In c
    
            ' v will be a String-type variant with our file name.
            ' Use it just like we would the return of Dir$.
    
    
        Next v
    
    
    
    Take Care,
    Elroy
    Last edited by Elroy; Jul 11th, 2018 at 09:15 AM.
    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.

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