Results 1 to 17 of 17

Thread: another listbox problem.. this one's tricky.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210

    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!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    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:
    1. Dim FileNames As Array
    2.         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.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    If you want to search in subfolders as well , then look up the search recursive method on this forum .

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    nah i just need the info on files in one directory.
    i tried using ms's example of
    VB Code:
    1. Dim dir As New DirectoryInfo("f:\windows\system32")
    2. Console.WriteLine(dir.Name)
    3. Console.WriteLine(dir.FullName)
    4. Console.WriteLine(dir.Root.ToString())
    5. Console.WriteLine("Number of subfolders: " & dir.GetDirectories().Length)
    6. 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.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. dir.GetDirectories().Length.[B]ToString()[/B]

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    thanks. that does exactly what i wanted.
    but i'm having another trouble with arrays.

    VB Code:
    1. Dim FileNames As Array
    2.         Dim FileNum As Int32
    3.         Dim dir As New IO.DirectoryInfo(Application.StartupPath & "\dat")
    4.         Dim i As Int32
    5.         Dim FileName As String
    6.         FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat")
    7.         FileNum = dir.GetFiles().Length.ToString()
    8.         For i = 0 To FileNum
    9.             FileName = IO.Path.GetFileNameWithoutExtension(FileNames(i))
    10.             lstCode.Items.Add(FileName)
    11.         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?

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this now
    VB Code:
    1. Dim FileNames As Array
    2.         Dim FileNum As Int32
    3.         Dim dir As New IO.DirectoryInfo(Application.StartupPath & "\dat")
    4.         Dim i As Int32
    5.         Dim FileName As String
    6.         FileNames = IO.Directory.GetFiles(Application.StartupPath & "\dat")
    7.         FileNum = dir.GetFiles().Length [B]-1[/B]
    8.         For i = 0 To FileNum
    9.             FileName = IO.Path.GetFileNameWithoutExtension(FileNames(i))
    10.             lstCode.Items.Add(FileName)
    11.         Next

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    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!

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
    			}

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    BTW , check this method also . It exists in any kind of array obj .
    VB Code:
    1. GetUpperBound

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    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.

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Dim FileNames() As String = IO.Directory.GetFiles(Application.StartupPath & "\*.dat")
    2.         For Each file As String In Filenames
    3.             lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file))
    4.         Next

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    Originally posted by Edneeis
    VB Code:
    1. Dim FileNames() As String = IO.Directory.GetFiles(Application.StartupPath & "\*.dat")
    2.         For Each file As String In Filenames
    3.             lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file))
    4.         Next
    i get an illegal character error on the first line

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim folder As String = IO.Path.Combine(Application.StartupPath, "dat")
    2.         Dim files() As String = IO.Directory.GetFiles(folder, "*.dat")
    3.         For Each file As String In files
    4.             lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file))
    5.         Next

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    i just wanted to load the names of all *.dat in \dat.
    thanks, it worked perfectly. pretty awesome.

  17. #17
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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:
    1. Dim folder As String = IO.Path.Combine(Application.StartupPath, "dat")
    2.         Dim files() As String = IO.Directory.GetFiles(folder, "*.dat")
    3.         For Each file As String In files
    4.             lstCode.Items.Add(IO.Path.GetFileNameWithoutExtension(file))
    5.         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
  •  



Click Here to Expand Forum to Full Width