|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
get list of file names in a directoryHow 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. "Dave B" <davidbarbe***@hotmail.com> wrote in message The simplest option is the Dir$ functionnews: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. -- Reply to the group so all can participate VB.Net: "Fool me once..." 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..." > "Dave B" <davidbarbe***@hotmail.com> wrote in message Did you read the help for Dir? The first time you call it you use thenews:%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? 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..." "Dave B" <davidbarbe***@hotmail.com> wrote in message Yes, but only once you've got it started :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? sFile = Dir( "<directory>\*.*" ) Do While sFile <> "" ' Do Something with this file, then sFile = Dir() ' no argument => get the next one Loop HTH, Phill W. 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 -- Show quoteHide quoteChris Hanscom - Microsoft MVP (VB) Veign's Resource Center http://www.veign.com/vrc_main.asp Veign's Blog http://www.veign.com/blog -- "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. > > "Dave B" <davidbarbe***@hotmail.com> wrote in how about this one....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. > > (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
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 Rather than perform all that string concatenation (which is one of the most 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 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
Show quote
Hide quote
"MikeD" <nob***@nowhere.edu> wrote in DOH !!!!!! I see that now, I did warn it was air code ! Shows what 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> > happens when you do things remoted in from work when your not supposed to be ! DanS Dave B wrote:
> How do I use VB to get the name of each file in a given directory? See http://vb.mvps.org/samples/DirDrill -- it's overkill for this precise problem,> (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. but will give you some ideas on how to use native VB for this sort of thing.
Is any device connected to COM1 port
Division by Zero Error - Help Urgent InStr search counter Merge array in VB Problem executing a DTS from a VB6 application after installing th Controlling location of form within form "_files" folders and related folders How to monopolize Windows in vb6 task bar? detect tab keypress on checkbox - how? |
|||||||||||||||||||||||