Home All Groups Group Topic Archive Search About

list of files in directories?

Author
23 Mar 2006 7:12 PM
Monica
I am trying to find a way to use "dir" to get a list of ALL the files in a
directory.  The problem for me is that this needs to be automated, not
knowing how many levels of subdirectories there are each time it is run.

Any sample code would be helpful.  All I know how to do is something like
this:

Dir(MainDocsPath, vbDirectory)

Of course that only returns a "." and I would have to keep coding embedded
dirs, but that's hard to do if one doesn't know how many levels deep they
are going.

THANKS

Author
23 Mar 2006 8:44 PM
DRBarkley
Show quote Hide quote
"Monica" <meisf***@supersky.com> wrote in message
news:u$ike2qTGHA.4436@TK2MSFTNGP10.phx.gbl...
> I am trying to find a way to use "dir" to get a list of ALL the files in a
> directory.  The problem for me is that this needs to be automated, not
> knowing how many levels of subdirectories there are each time it is run.
>
> Any sample code would be helpful.  All I know how to do is something like
> this:
>
> Dir(MainDocsPath, vbDirectory)
>
> Of course that only returns a "." and I would have to keep coding embedded
> dirs, but that's hard to do if one doesn't know how many levels deep they
> are going.
>
> THANKS
>
>

Monica,

Type Dir and press F1, then select Example.

It shows you how to get the next file in the directory.

HTH,

DRBarkley
Author
23 Mar 2006 9:10 PM
Monica
Yes, I am aware of that part.  What I was wondering is if there is a way to
loop through all directories and just return the FILE NAMES of every single
file in every directory, not knowing in advance how many directories deep
that is.  I am interested in more than just 1st or 2nd level directories and
the files in them.



Show quoteHide quote
"DRBarkley" <David.NOSPAMBarkley@L-3NOSPAMCom.com> wrote in message
news:O%23T0EqrTGHA.424@TK2MSFTNGP12.phx.gbl...
>
> "Monica" <meisf***@supersky.com> wrote in message
> news:u$ike2qTGHA.4436@TK2MSFTNGP10.phx.gbl...
> > I am trying to find a way to use "dir" to get a list of ALL the files in
a
> > directory.  The problem for me is that this needs to be automated, not
> > knowing how many levels of subdirectories there are each time it is run.
> >
> > Any sample code would be helpful.  All I know how to do is something
like
> > this:
> >
> > Dir(MainDocsPath, vbDirectory)
> >
> > Of course that only returns a "." and I would have to keep coding
embedded
> > dirs, but that's hard to do if one doesn't know how many levels deep
they
> > are going.
> >
> > THANKS
> >
> >
>
> Monica,
>
> Type Dir and press F1, then select Example.
>
> It shows you how to get the next file in the directory.
>
> HTH,
>
> DRBarkley
>
>
Author
23 Mar 2006 9:18 PM
Ken Halter
"Monica" <meisf***@supersky.com> wrote in message
news:e$fSl4rTGHA.1572@tk2msftngp13.phx.gbl...
> Yes, I am aware of that part.  What I was wondering is if there is a way
> to
> loop through all directories and just return the FILE NAMES of every
> single
> file in every directory, not knowing in advance how many directories deep
> that is.  I am interested in more than just 1st or 2nd level directories
> and
> the files in them.

Many ways..... here's one

DirDrill
http://vb.mvps.org/samples/project.asp?id=DirDrill

....and, another...

Minimal Code for a Recursive Search for Files
http://vbnet.mvps.org/code/fileapi/recursivefiles_minimal.htm

See the 'Related' section on that page for links to several variations of
this sample.

--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Author
24 Mar 2006 2:32 AM
Larry Serflaten
Show quote Hide quote
"Monica" <meisf***@supersky.com> wrote
> I am trying to find a way to use "dir" to get a list of ALL the files in a
> directory.  The problem for me is that this needs to be automated, not
> knowing how many levels of subdirectories there are each time it is run.
>
> Any sample code would be helpful.  All I know how to do is something like
> this:
>
> Dir(MainDocsPath, vbDirectory)
>
> Of course that only returns a "." and I would have to keep coding embedded
> dirs, but that's hard to do if one doesn't know how many levels deep they
> are going.
>
> THANKS


Paste the code below into a new form and view the results in the
Immediate window.  Change "D:\Temp\" to whatever folder you
are interested in.  See if that gets you a little farther along....

LFS




Private Sub Form_Load()
Dim List As New Collection

  List.Add "D:\Temp\"

  Do While List.Count
    ' Put code here to list files in List(1) folder
    Debug.Print List(1)
    FindSubs List
    List.Remove 1
  Loop
End Sub

Sub FindSubs(List As Collection)
Dim file As String
Dim path As String

  path = List(1)
  file = Dir(path & "*.*", vbDirectory)
  Do While Len(file)
    If Left(file, 1) <> "." Then
      If GetAttr(path & file) And vbDirectory Then
          List.Add path & file & "\"
      End If
    End If
    file = Dir()
  Loop
End Sub