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
Printable View
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
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 bunchQuote:
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
Pete
how abt this?
Code:Private Sub Command1_Click()
If Dir("C:\SomeDirName") = "" Then MkDir "C:\SomeDirName"
End Sub
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.
To Chris, your code is essentially the same as Aaron's with one exception; You're comparing two strings.Code:On Error Resume Next
MkDir "C:\YourDirectoryPath"
String operations in VB is so very slow that it's actually faster to call a function like Aaron does.
Take the following example:
Best regardsCode: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
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 :rolleyes:.Quote:
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"
I don't agree with you on this Matthew.Quote:
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 :rolleyes:.
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
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
nevermind, i cant do a mkdir "c:\blah\blahblah" without mkdir "c:\blah\" first...
Pete