Results 1 to 9 of 9

Thread: Working with directories

  1. #1

    Thread Starter
    Addicted Member LiquidRezin's Avatar
    Join Date
    Dec 2000
    Location
    Aliso Viejo, CA
    Posts
    176
    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

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Code:
    Private Sub Command1_Click()
        If Len(Dir("C:\SomeDirName\*.*", vbDirectory)) = 0 Then
            MkDir "C:\SomeDirName"
        End If
    End Sub

  3. #3

    Thread Starter
    Addicted Member LiquidRezin's Avatar
    Join Date
    Dec 2000
    Location
    Aliso Viejo, CA
    Posts
    176
    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

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    how abt this?

    Code:
    Private Sub Command1_Click()
        If Dir("C:\SomeDirName") = "" Then MkDir "C:\SomeDirName"
    End Sub

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

  6. #6
    Guest
    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 .

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

  8. #8

    Thread Starter
    Addicted Member LiquidRezin's Avatar
    Join Date
    Dec 2000
    Location
    Aliso Viejo, CA
    Posts
    176
    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

  9. #9

    Thread Starter
    Addicted Member LiquidRezin's Avatar
    Join Date
    Dec 2000
    Location
    Aliso Viejo, CA
    Posts
    176
    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
  •  



Click Here to Expand Forum to Full Width