i want to create a function that will scan a designated directory for specific files (in this case, *.dat), and then add the filenames (excluding the extension) in a listbox.
i have no idea where to begin... i appreciate ANY help.
thanks!
Printable View
i want to create a function that will scan a designated directory for specific files (in this case, *.dat), and then add the filenames (excluding the extension) in a listbox.
i have no idea where to begin... i appreciate ANY help.
thanks!
Check out the IO namespace everything you need is in there.
IO.Path.GetFilenameWithoutExtension will get the filename.
IO.Directory.GetFiles will get the files.
d'oh!
i always look in the wrong category. i was looking in io.file. :D
thanks.
EDIT:
i guess i'll have to deal with arrays and for...next loop here. but how can i acquire the number of files in a directory? this is what i have so far.
not sure if i'm even doing this right. i do get each filenames when i want filenames(0), filenames(1), etc.VB Code:
Dim FileNames As Array FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat")
If you want to search in subfolders as well , then look up the search recursive method on this forum .
nah i just need the info on files in one directory.
i tried using ms's example of
but i get "property access must assign to the property or uses of its value" error on dir.GetFiles().Length(). vb changed dir.GetFiles().Length to dir.GetFiles().Length() itself.VB Code:
Dim dir As New DirectoryInfo("f:\windows\system32") Console.WriteLine(dir.Name) Console.WriteLine(dir.FullName) Console.WriteLine(dir.Root.ToString()) Console.WriteLine("Number of subfolders: " & dir.GetDirectories().Length) Console.WriteLine("Number of files: " & dir.GetFiles().Length)
oh yeah. i did
Dim dir As New IO.DirectoryInfo("blehmydirectory") because i would get an error without IO.
VB Code:
dir.GetDirectories().Length.[B]ToString()[/B]
thanks. that does exactly what i wanted.
but i'm having another trouble with arrays.
VB Code:
Dim FileNames As Array Dim FileNum As Int32 Dim dir As New IO.DirectoryInfo(Application.StartupPath & "\dat") Dim i As Int32 Dim FileName As String FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat") FileNum = dir.GetFiles().Length.ToString() For i = 0 To FileNum FileName = IO.Path.GetFileNameWithoutExtension(FileNames(i)) lstCode.Items.Add(FileName) Next
i get an error at FileName = IO.Path.GetFileNameWithoutExtension(FileNames(i)) and it says "Index was outside the bounds of the array."
what's wrong with it? :confused:
Try this now
VB Code:
Dim FileNames As Array Dim FileNum As Int32 Dim dir As New IO.DirectoryInfo(Application.StartupPath & "\dat") Dim i As Int32 Dim FileName As String FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat") FileNum = dir.GetFiles().Length [B]-1[/B] For i = 0 To FileNum FileName = IO.Path.GetFileNameWithoutExtension(FileNames(i)) lstCode.Items.Add(FileName) Next
whoa. you're awesome.
i don't get the -1 part though.
i remember having to use that for my listbox. why does dir.GetFiles().Length generate one more than what's really there? weird.
but it works great. thanks!
Arrays start indexing from 0 and the GetDirectories() method returns the actual number of directories (remember we count things from 1 not 0 ) ;) .
a simple example in C# might clear things up :
Code:System.IO.DirectoryInfo dirInfo=new System.IO.DirectoryInfo("path");
//GetDirectories = 12
for (int i=0;i<dirInfo.GetDirectories().Length;i++)
{
//Here max value for i is 12 but executes 11 times
}
for (int i=0;i <=dirInfo.GetDirectories().Length-1;i++)
{
//Here max value for i is 11 and executes 11 times
}
BTW , check this method also . It exists in any kind of array obj .
VB Code:
GetUpperBound
baah that c# code is confusing but what you said made sense :)
thanks. so i guess same thing with the listbox since it starts with 0 too. bleh.
VB Code:
Dim FileNames() As String = IO.Directory.GetFiles(Application.StartupPath & "\*.dat") For Each file As String In Filenames lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file)) Next
i get an illegal character error on the first lineQuote:
Originally posted by Edneeis
VB Code:
Dim FileNames() As String = IO.Directory.GetFiles(Application.StartupPath & "\*.dat") For Each file As String In Filenames lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file)) Next
My bad I put the pattern in the path. Are you searching the subfolder 'dat' or for all dat file types? Or both? This shows if you are doing both:
VB Code:
Dim folder As String = IO.Path.Combine(Application.StartupPath, "dat") Dim files() As String = IO.Directory.GetFiles(folder, "*.dat") For Each file As String In files lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file)) Next
i just wanted to load the names of all *.dat in \dat.
thanks, it worked perfectly. pretty awesome. :)
hehe it happens to me all the time tooQuote:
Originally posted by Edneeis
My bad I put the pattern in the path. Are you searching the subfolder 'dat' or for all dat file types? Or both? This shows if you are doing both:
VB Code:
Dim folder As String = IO.Path.Combine(Application.StartupPath, "dat") Dim files() As String = IO.Directory.GetFiles(folder, "*.dat") For Each file As String In files lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file)) Next