|
-
Jul 31st, 2001, 08:13 AM
#1
Thread Starter
New Member
Check if an Diretory exists
I wonder how I can see if an Directory exists with an "if" statment
-
Jul 31st, 2001, 08:16 AM
#2
Member
VB Code:
If Len(Dir([i]"filename"[/i]) > 0 Then
' it exists
Else
' it doesn't
End If
Nobody better recommend that POS FileSystemObject.
-
Jul 31st, 2001, 08:24 AM
#3
Fanatic Member
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]
-
Jul 31st, 2001, 09:17 AM
#4
Junior Member
?
wot, the FileSystemObject from the sccrun.DLL file scripting object ? Tht FileSystemObject?
Why not ?
-
Jul 31st, 2001, 09:32 AM
#5
Fanatic Member
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.
-
Jul 31st, 2001, 09:43 AM
#6
Junior Member
antiFSOism...
Nobody better recommend that POS FileSystemObject.
You risk the wrath of Filburt1 ...
-
Jul 31st, 2001, 09:45 AM
#7
Member
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?
-
Jul 31st, 2001, 09:54 AM
#8
Fanatic Member
*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.
-
Jul 31st, 2001, 10:05 AM
#9
Member
Nicely said. 
It does have some advantages, like a ton of built-in functions.
-
Jul 31st, 2001, 10:16 AM
#10
Frenzied Member
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:
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.
-
Jul 31st, 2001, 10:34 AM
#11
hey
Can i have some cheese?
-
Jul 31st, 2001, 10:41 AM
#12
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:
Function DirExist(Path As String) As Boolean
If Right(Path, 1) <> "\" Then Path = Path & "\"
DirExist = Dir(Path) <> ""
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).
-
Jul 31st, 2001, 10:48 AM
#13
Fanatic Member
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?
-
Jul 31st, 2001, 10:50 AM
#14
Frenzied Member
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:
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.
-
Jul 31st, 2001, 10:57 AM
#15
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:
Function DirExist(ByVal Path As String) As Boolean
If Right(Path, 1) <> "\" Then Path = Path & "\"
DirExist = Dir(Path, vbDirectory) <> ""
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.
-
Jul 31st, 2001, 10:57 AM
#16
Frenzied Member
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.
-
Jul 31st, 2001, 11:00 AM
#17
Frenzied Member
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.
-
Jul 31st, 2001, 11:05 AM
#18
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
-
Jul 31st, 2001, 11:09 AM
#19
Frenzied Member
Ya true. Thanks tygur.
VB Code:
Function DirExist(ByVal Path As String) As Boolean
On Error Resume Next
If Right(Path, 1) <> "\" Then Path = Path & "\"
DirExist = Dir(Path, vbDirectory) <> ""
End Function
THIS IS THE FINAL VERSION
You just proved that sig advertisements work.
-
Jul 31st, 2001, 11:26 AM
#20
Fanatic Member
No need for error handling
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]
-
Jul 31st, 2001, 11:31 AM
#21
Frenzied Member
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.
-
Jul 31st, 2001, 12:29 PM
#22
Fanatic Member
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]
-
Jul 31st, 2001, 12:43 PM
#23
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
-
Jul 31st, 2001, 12:52 PM
#24
Fanatic Member
*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
-
Jul 31st, 2001, 01:02 PM
#25
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
-
Jul 31st, 2001, 01:05 PM
#26
Member
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!
-
Jul 31st, 2001, 01:05 PM
#27
Frenzied Member
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.
-
Jul 31st, 2001, 01:06 PM
#28
Fanatic Member
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]
-
Jul 31st, 2001, 01:06 PM
#29
-
Jul 31st, 2001, 01:08 PM
#30
Frenzied Member
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.
-
Jul 31st, 2001, 01:09 PM
#31
Fanatic Member
Well, my avatar should tell you all about my mental state
-
Jul 31st, 2001, 01:12 PM
#32
Member
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!
-
Jul 31st, 2001, 01:16 PM
#33
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.
-
Jul 31st, 2001, 01:19 PM
#34
Frenzied Member
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.
-
Jul 31st, 2001, 01:43 PM
#35
Fanatic Member
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]
-
Jul 31st, 2001, 01:56 PM
#36
Frenzied Member
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.
-
Jul 31st, 2001, 02:06 PM
#37
Frenzied Member
I DID!!!!!!!!!!!!!!!!!!!!!
VB Code:
Function DirExist(ByVal Path As String) As Boolean
On Error Resume Next
If Right(Path, 1) <> "\" Then Path = Path & "\"
DirExist = Dir(Path, vbDirectory) <> ""
End Function
Come on filburt dont flip over to the dark side (FSO)...
You just proved that sig advertisements work.
-
Jul 31st, 2001, 02:09 PM
#38
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.
-
Jul 31st, 2001, 02:10 PM
#39
Fanatic Member
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]
-
Jul 31st, 2001, 02:26 PM
#40
[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?
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
|