|
-
Dec 22nd, 2000, 01:50 PM
#1
Thread Starter
Addicted Member
can someone tell me how i can make a directory if it doesnt
exist? i know the structure is as follows:
if path doesnt exist then
mkdir "pathname"
but i dont know the commands in vb, thanks...
Pete
-
Dec 22nd, 2000, 02:10 PM
#2
Code:
Private Sub Command1_Click()
If Len(Dir("C:\SomeDirName\*.*", vbDirectory)) = 0 Then
MkDir "C:\SomeDirName"
End If
End Sub
-
Dec 22nd, 2000, 02:24 PM
#3
Thread Starter
Addicted Member
Originally posted by Aaron Young
Code:
Private Sub Command1_Click()
If Len(Dir("C:\SomeDirName\*.*", vbDirectory)) = 0 Then
MkDir "C:\SomeDirName"
End If
End Sub
that was simple heh, thanks a bunch
Pete
-
Dec 23rd, 2000, 04:14 AM
#4
PowerPoster
how abt this?
Code:
Private Sub Command1_Click()
If Dir("C:\SomeDirName") = "" Then MkDir "C:\SomeDirName"
End Sub
-
Dec 23rd, 2000, 05:37 AM
#5
Although Aaron allready given you the answer I would like to point out an alternative way of doing it.
It might not be the way you want to go in your project but here it is anyway.
You could simply treat an error as an exception and ignoring it.
That is try creating the directory. If the directory allready exists VB will through an error but you could simply ignore that.
Code:
On Error Resume Next
MkDir "C:\YourDirectoryPath"
To Chris, your code is essentially the same as Aaron's with one exception; You're comparing two strings.
String operations in VB is so very slow that it's actually faster to call a function like Aaron does.
Take the following example:
Code:
If MyString = "" Then
'Do Something
End If
'the above is slower then to check if the length of the string is zero
If Len(MyString) = 0 Then
'Do Something
End If
Best regards
-
Dec 23rd, 2000, 01:07 PM
#6
Originally posted by Joacim Andersson
Although Aaron allready given you the answer I would like to point out an alternative way of doing it.
It might not be the way you want to go in your project but here it is anyway.
You could simply treat an error as an exception and ignoring it.
That is try creating the directory. If the directory allready exists VB will through an error but you could simply ignore that.
Code:
On Error Resume Next
MkDir "C:\YourDirectoryPath"
Don't use that way, if you can catch the error without having to use the On Error Statement, than do so, because that is really not a good way at all .
-
Dec 27th, 2000, 08:46 AM
#7
Originally posted by Matthew Gates
Don't use that way, if you can catch the error without having to use the On Error Statement, than do so, because that is really not a good way at all .
I don't agree with you on this Matthew.
To treat errors as exceptions is an excellent way of writing a nice program flow.
It's used extensively in Java and C++.
I suppose it will be used more in VB with the next version when we get the try, catch and finally statements.
But until that we will still have to go with the On Error Resume Next statement.
I didn't say that this way was better than Aarons suggestion but I wouldn't say that his way is better either.
It's just two different solutions that accomplish the same thing.
Even so, my way cuts down the function calls to a minimum and therefore runs a little bit faster.
Best regards
-
Dec 29th, 2000, 04:19 PM
#8
Thread Starter
Addicted Member
quick question...when i create a subdirectory, do i have to create it's parent first?
would i have to do
mkdir "c:\blah"
mkdir "c:\blah\blahblah\"
or can i just do
mkdir "c:\blah\blahblah\"
?
Pete
-
Dec 29th, 2000, 04:24 PM
#9
Thread Starter
Addicted Member
nevermind, i cant do a mkdir "c:\blah\blahblah" without mkdir "c:\blah\" first...
Pete
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
|