Files in (sub)sudirectory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IS

    Files in (sub)sudirectory

    Hi,

    I am trying to list all the files in a directory and add them to a
    collection. As well, I am trying to add all the files to this collection
    that might be in subdirectories (if exist).

    Any hints to how I do this? With Dir$ and such, I can find only single files
    and directories. I guess I should be using API functions I am not aware of.

    Thanks for your help,
    IS


  • Bob Butler

    #2
    Re: Files in (sub)sudirector y

    "IS" <finansije@yaho o.com> wrote in message news:<bs02g4$ph k$1@shiva.neobe e.net>...[color=blue]
    > Hi,
    >
    > I am trying to list all the files in a directory and add them to a
    > collection. As well, I am trying to add all the files to this collection
    > that might be in subdirectories (if exist).
    >
    > Any hints to how I do this? With Dir$ and such, I can find only single files
    > and directories. I guess I should be using API functions I am not aware of.
    >[/color]

    you can use Dir$, just use the optional vbDirectory flag and test
    every file you get back using GetAttr. If it is a directory then save
    the path in a collection or other list. When Dir$ indicates no more
    check your list of directories and remove one to be processed next. a
    simple example:

    Dim sDir As String ' current directory
    Dim sDirsLeft As String ' directories to be processed
    Dim sFile As String ' current file
    Dim x As Integer ' scratch
    ' Note: the starting directory path MUST end with a \ character
    sDirsLeft = "C:\" & vbNullChar ' start with this directory
    Do While Len(sDirsLeft) ' any directories left?
    x = InStr(sDirsLeft , vbNullChar) ' get delimiter
    sDir = Left$(sDirsLeft , x - 1) ' peel off directory
    sDirsLeft = Mid$(sDirsLeft, x + 1) ' remove from to-do list
    sFile = Dir$(sDir & "*.*", vbDirectory) ' get first file
    Do While Len(sFile) ' any files left?
    If sFile <> "." And sFile <> ".." Then ' skip self-refs
    If GetAttr(sDir & sFile) And vbDirectory Then ' is it a
    directory?
    ' yes -- add to to-do-list
    sDirsLeft = sDir & sFile & "\" & vbNullChar & sDirsLeft
    Else
    If sFile Like "*.TXT" Then ' do we want this file?
    Debug.Print sDir & sFile ' process this file
    End If
    End If
    End If
    sFile = Dir$ ' get next file
    Loop
    DoEvents ' just to be nice!
    Loop

    A better option is to use the FindFirstFile/FindNextFile/FindClose API
    calls. I don't have an example handy but you can find samples at


    If you want the slowest possible method using the most overhead and a
    chance of it failing on systems randomly then use the FileSystemObjec t

    Comment

    • Steve Gerrard

      #3
      Re: Files in (sub)sudirector y


      "Bob Butler" <butlerbob@eart hlink.net> wrote in message
      news:fa10fb0.03 12191936.34427f 89@posting.goog le.com...[color=blue]
      >
      > If you want the slowest possible method using the most overhead and a
      > chance of it failing on systems randomly then use the FileSystemObjec t[/color]

      Three things to look for in every solution - if you are an IT executive,
      a government bureaucrat, or work for Microsoft. :)


      Comment

      • Randy Birch

        #4
        Re: Files in (sub)sudirector y

        Hey ... I resemble that ... I'm a government bureaucrat. <g>

        --

        Randy Birch
        MVP Visual Basic

        Please respond only to the newsgroups so all can benefit.


        "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
        news:FdmdncMUIK ugF3miRVn-tA@comcast.com. ..
        :
        : "Bob Butler" <butlerbob@eart hlink.net> wrote in message
        : news:fa10fb0.03 12191936.34427f 89@posting.goog le.com...
        : >
        : > If you want the slowest possible method using the most overhead and a
        : > chance of it failing on systems randomly then use the FileSystemObjec t
        :
        : Three things to look for in every solution - if you are an IT executive,
        : a government bureaucrat, or work for Microsoft. :)
        :
        :


        Comment

        • Steve Gerrard

          #5
          Re: Files in (sub)sudirector y

          Present company excluded, of course...

          "Randy Birch" <rgb_removethis @mvps.org> wrote in message
          news:_w0Fb.1248 16$%TO.75943@tw ister01.bloor.i s.net.cable.rog ers.com...[color=blue]
          > Hey ... I resemble that ... I'm a government bureaucrat. <g>
          >
          > --
          >
          > Randy Birch
          > MVP Visual Basic
          > http://www.mvps.org/vbnet/
          > Please respond only to the newsgroups so all can benefit.
          >
          >
          > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
          > news:FdmdncMUIK ugF3miRVn-tA@comcast.com. ..
          > :
          > : "Bob Butler" <butlerbob@eart hlink.net> wrote in message
          > : news:fa10fb0.03 12191936.34427f 89@posting.goog le.com...
          > : >
          > : > If you want the slowest possible method using the most overhead[/color]
          and a[color=blue]
          > : > chance of it failing on systems randomly then use the[/color]
          FileSystemObjec t[color=blue]
          > :
          > : Three things to look for in every solution - if you are an IT[/color]
          executive,[color=blue]
          > : a government bureaucrat, or work for Microsoft. :)
          > :
          > :
          >
          >[/color]


          Comment

          • IS

            #6
            Re: Files in (sub)sudirector y

            Exactly what I needed!
            Thank you, Bob!


            Comment

            Working...