|
-
Mar 6th, 2004, 01:56 AM
#1
Thread Starter
Addicted Member
another listbox problem.. this one's tricky.
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!
-
Mar 6th, 2004, 02:10 AM
#2
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.
-
Mar 6th, 2004, 02:35 AM
#3
Thread Starter
Addicted Member
d'oh!
i always look in the wrong category. i was looking in io.file.
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.
VB Code:
Dim FileNames As Array
FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat")
not sure if i'm even doing this right. i do get each filenames when i want filenames(0), filenames(1), etc.
Last edited by nahya^^; Mar 6th, 2004 at 02:59 AM.
-
Mar 6th, 2004, 03:06 AM
#4
Sleep mode
If you want to search in subfolders as well , then look up the search recursive method on this forum .
-
Mar 6th, 2004, 03:11 AM
#5
Thread Starter
Addicted Member
nah i just need the info on files in one directory.
i tried using ms's example of
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)
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.
oh yeah. i did
Dim dir As New IO.DirectoryInfo("blehmydirectory") because i would get an error without IO.
-
Mar 6th, 2004, 03:17 AM
#6
Sleep mode
VB Code:
dir.GetDirectories().Length.[B]ToString()[/B]
-
Mar 6th, 2004, 03:28 AM
#7
Thread Starter
Addicted Member
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?
-
Mar 6th, 2004, 03:32 AM
#8
Sleep mode
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
-
Mar 6th, 2004, 03:39 AM
#9
Thread Starter
Addicted Member
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!
-
Mar 6th, 2004, 03:54 AM
#10
Sleep mode
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
}
-
Mar 6th, 2004, 03:57 AM
#11
Sleep mode
BTW , check this method also . It exists in any kind of array obj .
-
Mar 6th, 2004, 03:57 AM
#12
Thread Starter
Addicted Member
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.
-
Mar 6th, 2004, 02:29 PM
#13
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
-
Mar 6th, 2004, 02:36 PM
#14
Thread Starter
Addicted Member
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
i get an illegal character error on the first line
-
Mar 6th, 2004, 02:45 PM
#15
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
-
Mar 6th, 2004, 02:50 PM
#16
Thread Starter
Addicted Member
i just wanted to load the names of all *.dat in \dat.
thanks, it worked perfectly. pretty awesome.
-
Mar 7th, 2004, 11:06 AM
#17
yay gay
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
hehe it happens to me all the time too
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|