Results 1 to 15 of 15

Thread: Dir function

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Dir function

    hi all,

    in vb6, when i required list of all txt files in a directory, i used the following code:

    VB Code:
    1. DirArray(1) = Dir("c:\mydir\*.txt")
    2.         For ArrCnt = 2 To UBound(DirArray) - 1
    3.             DirArray(ArrCnt) = Dir() 'here's the problem with dir function
    4.         Next

    but in vb.net, the dir function is not working without the pathname. in my xp pc, which is my development pc, no error is generated. but in client pc, which is win 98 se, it shows the error.

    pls guide, what to do?

    regards,

    prakash

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Use the static Directory Class to get files with specific pattern .
    VB Code:
    1. Dim files() As String = IO.Directory.GetFiles("c:\", "*.txt")
    2.         For Each file As String In files
    3.             MessageBox.Show(file)
    4.         Next

  3. #3
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224
    Hi,

    Imports System
    Imports System.IO

    ListBox1.DataSource = Directory.GetFiles("C:\")

    Have a nice day

  4. #4

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    thanx for ur responses.

    but just for info, anybody please tell me why the error was not thrown on xp pc, unlike win 98se.

    regards

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What's the error message ?

  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    it gives some error that 'you cannot use dir() function without pathname.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by pvbangera
    it gives some error that 'you cannot use dir() function without pathname.
    Umm , you mean it complains if you provided a variable instead of pathname ? I'm afraid I couldn't get it .

  8. #8

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    no, not like that. if i use dir() function without any arguments, then it shows error.

    VB Code:
    1. strPath = "c:\temp\*.txt"
    2. strFile = dir(strPath) 'No problem
    3. strFile = dir() 'Error occurs

    regards

    prakash

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by pvbangera

    VB Code:
    1. strFile = dir() 'Error occurs
    When you use it this way , what do you think dir() function would return ???????? .

    You must specify path name .

  10. #10

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    my dear friend,

    i need not tell u that in vb6 when u say
    VB Code:
    1. Text1 = dir("c:\*.txt")
    it will return 1st txt file in the path and next time when u say
    VB Code:
    1. Text1 = dir()    'without any argument
    it returns next txt file in the directory. i want to say that this is not working in vb.net.

    i hope, i am clear.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    eeh , Do you really have to use that function (which is basically for compatibility code with VB6) . Have you checked IO.Directory.GetFiles() function ??

  12. #12
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pvbangera
    i want to say that this is not working in vb.net.
    well well! thank you for your information, but I think people knwo that hehe
    my suggestion as well as others, dont use the dir() function, its just for backwards compatibility I believe. Try to only use the classes provided in the .NET framework (excluding the visualbasic compatibility crap). yulyos and Pirate already told you how to use them


    btw pirate you're getting used to start your sentences with "eeh" too ha? I dont know where I got that habit from but it seems like it's getting into your too
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  13. #13

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961
    eeh (me too)

    my purpose was solved by pirate's 1st post. but i had a question after that which i guess u ppl forgot.

    but just for info, anybody please tell me why the error was not thrown on xp pc, unlike win 98se.
    i dont want to use dir() function anymore. just for info sake i want the answer for the above question. thats all

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by MrPolite


    btw pirate you're getting used to start your sentences with "eeh" too ha? I dont know where I got that habit from but it seems like it's getting into your too
    hehe , it's like plague over these forums . Could be VBF-Specific ?

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by pvbangera
    my purpose was solved by pirate's 1st post. but i had a question after that which i guess u ppl forgot.

    i dont want to use dir() function anymore. just for info sake i want the answer for the above question. thats all
    I don't know but it might be dumb enough if I say it has some issue with the OS and FS (NTFS vs FAT32) . I don't know what inside this function . I just check MSDN , and there's no note about supporting OS .

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