Home All Groups Group Topic Archive Search About

get list of file names in a directory

Author
19 Oct 2005 1:09 PM
Dave B
How do I use VB to get the name of each file in a given directory?  (Once I
get the filenames I can put them into a string array on my own, I just don't
know how to get them using VB.)  Thanks.

Author
19 Oct 2005 1:07 PM
Bob Butler
"Dave B" <davidbarbe***@hotmail.com> wrote in message
news:e3ZV9zK1FHA.2576@TK2MSFTNGP10.phx.gbl
> How do I use VB to get the name of each file in a given directory?
> (Once I get the filenames I can put them into a string array on my
> own, I just don't know how to get them using VB.)  Thanks.

The simplest option is the Dir$ function

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
19 Oct 2005 1:27 PM
Dave B
When I do:

For i = 1 To 1000
        strFiles(i) = Dir("c:\DavesData\10Ks\")
Next i

it fills the array with the name of the first file in the directory.  Isn't
the Dir function supposed to give the name of the next filename each time
you call it?  What did I do wrong?



Show quoteHide quote
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:eIIVf4K1FHA.2312@TK2MSFTNGP14.phx.gbl...
> "Dave B" <davidbarbe***@hotmail.com> wrote in message
> news:e3ZV9zK1FHA.2576@TK2MSFTNGP10.phx.gbl
> > How do I use VB to get the name of each file in a given directory?
> > (Once I get the filenames I can put them into a string array on my
> > own, I just don't know how to get them using VB.)  Thanks.
>
> The simplest option is the Dir$ function
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
Author
19 Oct 2005 1:38 PM
Bob Butler
"Dave B" <davidbarbe***@hotmail.com> wrote in message
news:%23WPIO%23K1FHA.4032@TK2MSFTNGP15.phx.gbl
> When I do:
>
> For i = 1 To 1000
>         strFiles(i) = Dir("c:\DavesData\10Ks\")
> Next i
>
> it fills the array with the name of the first file in the directory.
> Isn't the Dir function supposed to give the name of the next filename
> each time you call it?  What did I do wrong?

Did you read the help for Dir?  The first time you call it you use the
arguments to specify what you want.  You then use it without arguments to
get the next file.

You are also apparently assuming exactly 1000 files in the directory... what
happens when there are more or less?  You should either use ReDim to adjust
the array or a collection to hold the names.

dim strFiles() as string
dim strFile as string
dim i as long
redim strFiles(1 to 100)
strFile=Dir$("C:\davesdata\10ks\*.*")
do while len(strFile)>0
  i=i+1
  if i>ubound(strFiles) then redim preserve strFiles(1 to i+50)
  strFiles(i)=strFile
  strFile=Dir$
loop
if i=0 then
  redim strFiles(0 to 0)
elseif i<>ubound(strFiles) then
  redim preserve strFiles(1 to i)
end if
for i=1 to ubound(strFiles)
  debug.print strFiles(i)
next

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
20 Oct 2005 11:48 AM
Phill. W
"Dave B" <davidbarbe***@hotmail.com> wrote in message
news:%23WPIO%23K1FHA.4032@TK2MSFTNGP15.phx.gbl...
> Isn't the Dir function supposed to give the name of the next filename
> each time you call it?

Yes, but only once you've got it started :

sFile = Dir( "<directory>\*.*" )
Do While sFile <> ""
    ' Do Something with this file, then

    sFile = Dir() ' no argument => get the next one
Loop

HTH,
    Phill  W.
Author
19 Oct 2005 1:25 PM
Veign
Minimal Code for a Single or Multiple File Extension Recursive Search for
Files (single drive):
http://vbnet.mvps.org/index.html?code/fileapi/recursivefiles_minimal_multiple.htm


--
Chris Hanscom - Microsoft MVP (VB)
Veign's Resource Center
http://www.veign.com/vrc_main.asp
Veign's Blog
http://www.veign.com/blog
--


Show quoteHide quote
"Dave B" <davidbarbe***@hotmail.com> wrote in message
news:e3ZV9zK1FHA.2576@TK2MSFTNGP10.phx.gbl...
> How do I use VB to get the name of each file in a given directory?  (Once
> I
> get the filenames I can put them into a string array on my own, I just
> don't
> know how to get them using VB.)  Thanks.
>
>
Author
19 Oct 2005 4:00 PM
DanS
"Dave B" <davidbarbe***@hotmail.com> wrote in
news:e3ZV9zK1FHA.2576@TK2MSFTNGP10.phx.gbl:

> How do I use VB to get the name of each file in a given directory?
> (Once I get the filenames I can put them into a string array on my
> own, I just don't know how to get them using VB.)  Thanks.
>
>

how about this one....
(Warning! (i'm going to steal the word) AIRCODE!! (my apologies))

----------------------
Dim fileName as string
Dim allFiles() as string
dim tFileName as string

filename = Dir("c:\whatever\")

do until len filename = 0
        tfilename = tfilename & "|" 'that is the pipe char
        filename = dir()
loop

if len(tfilename) > 0 then
        allFiles() = split(tfilename, "|")
endif
-----------------------

you should end up with an array of filenames. no path in this
example.also, it may be necessary to examine the return of the dir before
adding it to tFilename.

regards,

DanS
Author
19 Oct 2005 5:16 PM
MikeD
Show quote Hide quote
"DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
news:Xns96F47AFF4B698idispcom@216.196.97.142...
>
> how about this one....
> (Warning! (i'm going to steal the word) AIRCODE!! (my apologies))
>
> ----------------------
> Dim fileName as string
> Dim allFiles() as string
> dim tFileName as string
>
> filename = Dir("c:\whatever\")
>
> do until len filename = 0
>    tfilename = tfilename & "|" 'that is the pipe char
>    filename = dir()
> loop
>
> if len(tfilename) > 0 then
>    allFiles() = split(tfilename, "|")
> endif

Rather than perform all that string concatenation (which is one of the most
inefficient things VB does), you'd be better off adding to the array within
the loop.  You don't need to ReDim Preserve (which is time-consuming as
well) the array every iteration. Instead, just do it every nth iteration and
then one final time (to get the array to the correct size) when the loop
terminates. Plus, you've got a bug in your code.  All you're doing is
assigning pipe characters to tfilename.  <g>

--
Mike
Microsoft MVP Visual Basic
Author
20 Oct 2005 12:25 AM
DanS
Show quote Hide quote
"MikeD" <nob***@nowhere.edu> wrote in
news:uRMVYDN1FHA.736@tk2msftngp13.phx.gbl:

>
> "DanS" <t.h.i.s.n.t.h.a.t@a.d.e.l.p.h.i.a..n.e.t> wrote in message
> news:Xns96F47AFF4B698idispcom@216.196.97.142...
>>
>> how about this one....
>> (Warning! (i'm going to steal the word) AIRCODE!! (my apologies))
>>
>> ----------------------
>> Dim fileName as string
>> Dim allFiles() as string
>> dim tFileName as string
>>
>> filename = Dir("c:\whatever\")
>>
>> do until len filename = 0
>>    tfilename = tfilename & "|" 'that is the pipe char
>>    filename = dir()
>> loop
>>
>> if len(tfilename) > 0 then
>>    allFiles() = split(tfilename, "|")
>> endif
>
> Rather than perform all that string concatenation (which is one of the
> most inefficient things VB does), you'd be better off adding to the
> array within the loop.  You don't need to ReDim Preserve (which is
> time-consuming as well) the array every iteration. Instead, just do it
> every nth iteration and then one final time (to get the array to the
> correct size) when the loop terminates. Plus, you've got a bug in your
> code.  All you're doing is assigning pipe characters to tfilename.
> <g>
>

DOH !!!!!! I see that now, I did warn it was air code ! Shows what
happens when you do things remoted in from work when your not supposed to
be !

DanS
Author
19 Oct 2005 9:10 PM
Karl E. Peterson
Dave B wrote:
> How do I use VB to get the name of each file in a given directory?
> (Once I get the filenames I can put them into a string array on my
> own, I just don't know how to get them using VB.)  Thanks.

See http://vb.mvps.org/samples/DirDrill -- it's overkill for this precise problem,
but will give you some ideas on how to use native VB for this sort of thing.
--
Working Without a .NET?
http://classicvb.org/petition