Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: Check if an Diretory exists

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Sweden
    Posts
    15

    Question Check if an Diretory exists

    I wonder how I can see if an Directory exists with an "if" statment

  2. #2
    VB Code:
    1. If Len(Dir([i]"filename"[/i]) > 0 Then
    2.     ' it exists
    3. Else
    4.     ' it doesn't
    5. End If

    Nobody better recommend that POS FileSystemObject.

  3. #3
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Sample Function

    Try this...

    Code:
    Private Function DirectoryExists(ByVal strDirectory As String) As Boolean
    
    If (Right(strDirectory, 1) <> "\") Then
            strDirectory = strDirectory & "\"
    End If
    
    If Dir(strDirectory, vbDirectory) <> "" Then
        DirectoryExists = True
    Else
        DirectoryExists = False
    End If
    
    End Function
    
    'Usage
    Msgbox DirectoryExists("C:\Windows")
    Hope this helps... Oh, and you might want to look into the FSO...

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  4. #4
    Junior Member
    Join Date
    Jul 2001
    Location
    Dublin
    Posts
    27

    ?

    wot, the FileSystemObject from the sccrun.DLL file scripting object ? Tht FileSystemObject?
    Why not ?



  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Indeed. Add the Microsoft Scripting Runtime to your References:
    Code:
    Dim FSO As New FileSystemObject
    
    MsgBox FSO.FolderExists("C:\TestMe")
    Before the FSO flame wars hit again, this is just an alternate method to file ops.

    The Dir() Method works just as well.
    -Excalibur

  6. #6
    Junior Member
    Join Date
    Jul 2001
    Location
    Dublin
    Posts
    27

    antiFSOism...

    Nobody better recommend that POS FileSystemObject.

    You risk the wrath of Filburt1 ...

  7. #7

    Re: ?

    Originally posted by Timwit
    wot, the FileSystemObject from the sccrun.DLL file scripting object ? Tht FileSystemObject?
    Why not ?


    Don't add unneeded references to a project. VB beginners 101. Simple logic. If the built in functions work, why import a more complex and larger beast?

  8. #8
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    *chuckle* As you can see, some people prefer it, others don't, much like cheese or spinach. If you want to keep your distribution size down, don't use the FSO even though it is a small 145k as opposed to some of the other controls which can hit a meg or more.

    It boils down to personal preference If you want the functionality of the FSO but not the reference to your project, write your own class or module that encapsulates the intrinsic functions you need in methods.
    -Excalibur

  9. #9
    Nicely said.

    It does have some advantages, like a ton of built-in functions.

  10. #10
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I would tend to agree with Filburt...theres no need for FSO unless your going to use a lot of its functions. BTW heres a more compact funciton that does that same thing as the code in the previous posts.
    VB Code:
    1. The code that was here was wrong!! look way down for the real code

    This code does exactly the same thing as Digitral-X-Treme's code...only its 2 lines intead of 10 .
    Last edited by nishantp; Jul 31st, 2001 at 11:11 AM.
    You just proved that sig advertisements work.

  11. #11
    Dimension
    Guest
    hey

    Can i have some cheese?

  12. #12
    Tygur
    Guest
    Originally posted by nishantp
    I would tend to agree with Filburt...theres no need for FSO unless your going to use a lot of its functions. BTW heres a more compact funciton that does that same thing as the code in the previous posts.
    VB Code:
    1. Function DirExist(Path As String) As Boolean
    2. If Right(Path, 1) <> "\" Then Path = Path & "\"
    3. DirExist = Dir(Path) <> ""
    4. End Function

    This code does exactly the same thing as Digitral-X-Treme's code...only its 2 lines intead of 10 .
    Do yourself a favor and test your code before posting it

    First off, change it so Path is passed by value, since you're changing it within the function:
    Function DirExist(ByVal Path As String) As Boolean

    ..but that's not why I'm posting this message. The reason why the other function takes up so many more lines is because it actually checks for the existence of a directory, not a file. All your function will look for is a file. Test it out, and it will insist that c:\msdos.sys exists (if you're running win9x), but c:\windows doesn't (whether or not it's your windows directory).

  13. #13
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    The fact that VB passes ByRef as default is a little cheesy in my book. But I'd have to agree, pass ByVal unless you want errors cropping up. I still like FSO, but that's because I use alot of it's functions

    Dimension: Sure, do you want Cheddar, Swiss, Wesleydale (sp?), Bleu, or Fetta?
    -Excalibur

  14. #14
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Sorry, sorry, I did test it...just not enough. The above code it wrong, it works but only on root directories...not sub dirs. This code does the trick (has been tested properly) :
    VB Code:
    1. The code that was here was wrong!! look down for the real code...
    Thanks for the pointers Tygur
    Last edited by nishantp; Jul 31st, 2001 at 11:10 AM.
    You just proved that sig advertisements work.

  15. #15
    Tygur
    Guest
    Originally posted by nishantp
    Sorry, sorry, I did test it...just not enough. The above code it wrong, it works but only on root directories...not sub dirs. This code does the trick (has been tested properly) :
    VB Code:
    1. Function DirExist(ByVal Path As String) As Boolean
    2. If Right(Path, 1) <> "\" Then Path = Path & "\"
    3. DirExist = Dir(Path, vbDirectory) <> ""
    4. End Function
    Thanks for the pointers Tygur
    I'm guessing you didn't test its reaction to files

    It will return true if you give it a file. Since it's supposed to check for the existence of a directory, not a file, it should return false.

    EDIT: I just tested it myself, and I found that it gives an error if you give it a filename, instead of just returning true. The function posted by [Digital-X-Treme] does the same thing.

  16. #16
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I agree...the default should be ByVal..i mean how many times do we use ByRef's capabilities compared with ByVal?

    PS - you forgot to offer Motzerella Excalibur...
    You just proved that sig advertisements work.

  17. #17
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by Tygur

    I'm guessing you didn't test its reaction to files

    It will return true if you give it a file. Since it's supposed to check for the existence of a directory, not a file, it should return false.
    Yeah but im guessing the person knows whether there testing for the existance of a file or a directory. My code gives an error if a filename is provided. Lol and im 2 lazy to bother writing code to fix that...
    You just proved that sig advertisements work.

  18. #18
    Tygur
    Guest
    Originally posted by nishantp

    Yeah but im guessing the person knows whether there testing for the existance of a file or a directory. My code gives an error if a filename is provided. Lol and im 2 lazy to bother writing code to fix that...
    Actually, the code to fix it is very simple. Just put this line as the first line of the function:
    On Error Resume Next

  19. #19
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Ya true. Thanks tygur.
    VB Code:
    1. Function DirExist(ByVal Path As String) As Boolean
    2. On Error Resume Next
    3. If Right(Path, 1) <> "\" Then Path = Path & "\"
    4. DirExist = Dir(Path, vbDirectory) <> ""
    5. End Function


    THIS IS THE FINAL VERSION
    You just proved that sig advertisements work.

  20. #20
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    No need for error handling

    Why do you need the error handling? The function i posted, works without error checking, as it checks the return from the dir function... Try it.

    Code:
    Private Function DirectoryExists(ByVal strDirectory As String) As Boolean
    
    If (Right(strDirectory, 1) <> "\") Then
            strDirectory = strDirectory & "\"
    End If
    
    If Dir(strDirectory, vbDirectory) <> "" Then
        DirectoryExists = True
    Else
        DirectoryExists = False
    End If
    
    End Function
    
    MsgBox DirectoryExists("afendows")  'False
    MsgBox DirectoryExists("C:\Windows\") 'True
    MsgBox DirectoryExists("C:\Windows")       'True
    MsgBox DirectoryExists("@")     'False


    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  21. #21
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Lol Tygur just pointed all that out Digital...try pointing your function to a file instead of a dir. It'll give an error.

    Also...our functions are doing exactly the same thing. Your doing If dir(FilePath, vbDirectory) <>"" then ... so if dir(FilePath, vbDirectory) <>"" evaluates to true, then you hard coded it to give true. I just got rid of the if statement all together because your doing the same thing twice. The statement will either return true of false anyways. I know i could have explained it better...but thats the concept.
    You just proved that sig advertisements work.

  22. #22
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    It works!

    nishantp, the function i supplied DOES NOTreturn an error when checking for a file...

    The function i supplied specifically uses the vbDirectory attribute to check if the target is 'directory related'. If it isn't, then it returns false!

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  23. #23
    Tygur
    Guest

    Re: It works!

    Originally posted by [Digital-X-Treme]
    nishantp, the function i supplied DOES NOTreturn an error when checking for a file...

    The function i supplied specifically uses the vbDirectory attribute to check if the target is 'directory related'. If it isn't, then it returns false!

    Laterz
    Out of curiosity, did you test it at all to see what happens when you give it a file? I doubt it. I suggest you go ahead and test it before you make an even bigger fool of yourself

  24. #24
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    *chuckle*
    "Runtime Error 52: Bad Filename or Number"
    Code:
        Dim fso As New FileSystemObject
        MsgBox fso.FolderExists(fso.GetParentFolderName("C:\program files\desktop.ini"))
    Returns True because I'm looking for the parent folder name.
    Code:
        Dim fso As New FileSystemObject
        MsgBox fso.FolderExists("C:\program files\desktop.ini")
    Returns False because I specified a file.
    No errors occured. FSO has some uses, and I say add it to your references for every project! MUAHAHAHAHAHA
    -Excalibur

  25. #25
    Tygur
    Guest
    nishantp's final copy of the DirExist function works just as well. It's just [Digital-X-Treme]'s function that returns the error now

    FSO is not needed. Everyone, please ignore ExcalibursZone's post

  26. #26
    Originally posted by ExcalibursZone
    ...FSO has some uses, and I say add it to your references for every project! MUAHAHAHAHAHA
    *knocks out ExcalibursZone and starts kicking him* NO FSO! NO FSO!

  27. #27
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Re: It works!

    Originally posted by [Digital-X-Treme]
    nishantp, the function i supplied DOES NOTreturn an error when checking for a file...

    The function i supplied specifically uses the vbDirectory attribute to check if the target is 'directory related'. If it isn't, then it returns false!

    Laterz
    hello? anybody home?Have you even bothered trying it out?? I gives an error for files!! I mean ok i didnt test mine properly at first. But after someone tells me it doesnt work i tested it and made the changes. And if you look at both our functions properly you will notice that they do exactly the same thing!!
    You just proved that sig advertisements work.

  28. #28
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Question Guilty Until Proven Innocent?

    Hmmm....
    Originally posted by Tygur
    Out of curiosity, did you test it at all to see what happens when you give it a file?
    Yes i did test it. I test all my code before posting, thoroughly. I have used this function in a number of my projects.

    Originally posted by Tygur
    I doubt it.
    Why?!

    Originally posted by Tygur
    I suggest you go ahead and test it before you make an even bigger fool of yourself
    OK. Just to PROVE i have 'tested it' before i make an even 'bigger fool of myself' as you put it, here is some results.

    Code:
    Private Sub Form_Load()
    
    ' 'C:\Command.com' - File
    ' Predicted return: False
    MsgBox DirectoryExists("C:\Command.com")
    ' Return: False
    ' This is correct, as the file isn't a directory, it's a file...
    
    ' 'C:\Windows' - Directory
    ' Predicted return: True
    MsgBox DirectoryExists("C:\Windows")
    ' Return: True
    ' This is correct, as the directory is a directory, and NOT a file.
    End Sub
    
    Private Function DirectoryExists(ByVal strDirectory As String) As Boolean
    
    If (Right(strDirectory, 1) <> "\") Then
            strDirectory = strDirectory & "\"
    End If
    
    If Dir(strDirectory, vbDirectory) <> "" Then
        DirectoryExists = True
    Else
        DirectoryExists = False
    End If
    
    End Function
    The function works for me. You are obviously getting some sort of error. Please post the code you are using and the results you get from calling the function and the argument so i can see whats going on.

    Anyone else got any contributions on whether the function works?

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  29. #29
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    *ROFL* it does indeed work well Kudos to nishantp (kudos ... mmm reminds me of mail order monsters ...)

    I like FSO :P but to each his own
    -Excalibur

  30. #30
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Originally posted by ExcalibursZone
    *chuckle*
    "Runtime Error 52: Bad Filename or Number"
    Code:
        Dim fso As New FileSystemObject
        MsgBox fso.FolderExists(fso.GetParentFolderName("C:\program files\desktop.ini"))
    Returns True because I'm looking for the parent folder name.
    Code:
        Dim fso As New FileSystemObject
        MsgBox fso.FolderExists("C:\program files\desktop.ini")
    Returns False because I specified a file.
    No errors occured. FSO has some uses, and I say add it to your references for every project! MUAHAHAHAHAHA
    Nooooooooo!! See what we've done!! Were arguing over stupid stuff like this and Excalibur is laughing his head off with his FSO...Listen people FSO is a cult. Dont let yourself be sucked in...
    You just proved that sig advertisements work.

  31. #31
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Well, my avatar should tell you all about my mental state
    -Excalibur

  32. #32
    Originally posted by nishantp
    FSO is a cult. Dont let yourself be sucked in...
    Damn straight! Don't let the bytes get you! Look what they did to me! I mutated into a turtle!

  33. #33
    Tygur
    Guest

    Re: Guilty Until Proven Innocent?

    Originally posted by [Digital-X-Treme]
    The function works for me. You are obviously getting some sort of error. Please post the code you are using and the results you get from calling the function and the argument so i can see whats going on.

    Anyone else got any contributions on whether the function works?

    Laterz
    Does c:\command.com really exist? If you have win9x/me, try it on c:\msdos.sys (you probably do, since you expect command.com to be in c:\). Basically make sure the file you're testing it with really exists.

    I'm getting the error no matter what file I try it on.

  34. #34
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375

    Re: Guilty Until Proven Innocent?

    Originally posted by [Digital-X-Treme]
    Hmmm....

    Yes i did test it. I test all my code before posting, thoroughly. I have used this function in a number of my projects.


    Why?!


    OK. Just to PROVE i have 'tested it' before i make an even 'bigger fool of myself' as you put it, here is some results.

    Code:
    Private Sub Form_Load()
    
    ' 'C:\Command.com' - File
    ' Predicted return: False
    MsgBox DirectoryExists("C:\Command.com")
    ' Return: False
    ' This is correct, as the file isn't a directory, it's a file...
    
    ' 'C:\Windows' - Directory
    ' Predicted return: True
    MsgBox DirectoryExists("C:\Windows")
    ' Return: True
    ' This is correct, as the directory is a directory, and NOT a file.
    End Sub
    
    Private Function DirectoryExists(ByVal strDirectory As String) As Boolean
    
    If (Right(strDirectory, 1) <> "\") Then
            strDirectory = strDirectory & "\"
    End If
    
    If Dir(strDirectory, vbDirectory) <> "" Then
        DirectoryExists = True
    Else
        DirectoryExists = False
    End If
    
    End Function
    The function works for me. You are obviously getting some sort of error. Please post the code you are using and the results you get from calling the function and the argument so i can see whats going on.

    Anyone else got any contributions on whether the function works?

    Laterz
    Look man i copied and pasted your code (i doubt even my computer could screw that up). I used the function to point to "c:\winnt\win.ini" which exists. And it gave me error 52 "Bad Filename or Number". Your function adds the "\" to the end of "C:\winnt\win.ini" to make "C:\winnt\win.ini\" And it gives the error. You could hardcode a way out of that but instead Tygur told me the easier way: On Error Resume Next. Come on man my function is 3 lines long. it shouldnt be very hard to understand here...
    You just proved that sig advertisements work.

  35. #35
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Jeesus

    I used the function to point to "c:\winnt\win.ini" which exists. And it gave me error 52 "Bad Filename or Number". Your function adds the "\" to the end of "C:\winnt\win.ini" to make "C:\winnt\win.ini\" And it gives the error. You could hardcode a way out of that but instead Tygur told me the easier way: On Error Resume Next. Come on man my function is 3 lines long. it shouldnt be very hard to understand here...
    Yeah, *Cough, it aint that hard to understand...

    Your function adds the "\" to the end of "C:\winnt\win.ini" to make "C:\winnt\win.ini\" And it gives the error...
    My function will check if a DIRECTORY, named "C:\winnt\win.ini\" exists. On your system, this directory doesn't exist, so the function should return false, with no errors.

    Look, this is getting stupid. Fact is, it works for me (using Win 98 SE, VB6 Pro, SP4), and i dont give a **** if it doesn't work for you. Patronise me all you like, but i know i am correct

    I noted you are using NT... Could be your problem...

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  36. #36
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Im not exactly the only one. Tygur and Excalibur also tried your code and got the same results (Runtime error 52). But of course...you still know your right...(just i little ignorant there pal)...
    You just proved that sig advertisements work.

  37. #37
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I DID!!!!!!!!!!!!!!!!!!!!!
    VB Code:
    1. Function DirExist(ByVal Path As String) As Boolean
    2. On Error Resume Next
    3. If Right(Path, 1) <> "\" Then Path = Path & "\"
    4. DirExist = Dir(Path, vbDirectory) <> ""
    5. End Function

    Come on filburt dont flip over to the dark side (FSO)...
    You just proved that sig advertisements work.

  38. #38
    Tygur
    Guest

    Re: Jeesus

    Originally posted by [Digital-X-Treme]
    Look, this is getting stupid. Fact is, it works for me (using Win 98 SE, VB6 Pro, SP4), and i dont give a **** if it doesn't work for you. Patronise me all you like, but i know i am correct

    I noted you are using NT... Could be your problem...

    Laterz
    Actually, you probably should care about whether or not it works on other people's computers. You should try your best to make sure your programs work on as many computers as possible.

    I tested it out in Win98 and it does return false, so this does look like a WinNT/2000 (and very likely XP) issue. But I think you should care that it doesn't work right on non-Win9X/ME computers.

  39. #39
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Dude...

    Look, nishantp dude... After reading another post on directory attributes (http://www.vbforums.com/showthread.p...threadid=93125), it's obvious that this is a platform specific problem. NT/Win2000 seems to have problems with directory and file attributes in VB, but Win 98 and 95 doesn't. Basically, the function will work for me, but not for you guys, because...

    ExcalibursZone - Running Win2k.
    nishantp - Running NT
    Tygur - Running Win 2K

    nishantp, i wasn't being ignorant, i am just trying to get my point across to people who have been patronising me throughout this post, trying to get their points over...

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  40. #40
    Tygur
    Guest
    [Digital-X-Treme],
    Both our posts came at about the same time, and I just want to make sure you saw my post. Did you see it?

Page 1 of 2 12 LastLast

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