CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2000
    Posts
    24

    Array and build nested directory

    Question:
    1.aryFileName = Array("n01.dbf", "02.dbf", "n03.dbf", "n04.dbf", "n05.dbf")

    The code above is define an array of 5 elements. What happen if there are 100 elements? How can I do it in a 'FOR' loop or is there any better solution?


    2. How can I created a nested directory?

    txtTrgPath is a text box for user to input the pathname

    Dim inBackPos As Integer, inForePos As Integer
    Dim path
    inBackPos = 3
    ChDrive txtTrgPath 'Change to current drive
    ChDir "\" 'Change to current directory
    inForePos = InStr(4, txtTrgPath, "\")
    Do While inForePos <> 0
    path = Mid(txtTrgPath, inBackPos + 1, inForePos - inBackPos - 1)
    MkDir path
    ChDir path
    inBackPos = inForePos
    inForePos = InStr(inBackPos + 1, txtTrgPath, "\")
    Loop

    The code above is correct if the directory 'Dir', 'Path' and 'Path1', let say c:\Dir\Path\Path1\ doesn't exist

    What happen if the directory 'Dir' exist and the directory 'Path' and 'Path1' doesn't exist? How can I correct the problem?


    Thanks
    Andy


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Array and build nested directory

    1)

    for t=0 to 99
    ' note: your 100th file will be called "n100.dbf", the number will then contain 3 digits
    if t < 9 then
    ' if to small, add a leading 0
    aryFileName(t) = "n0" & t + 1 & ".dbf"
    else
    aryFileName(t) = "n" & t + 1 & ".dbf"
    next t




    2) add a errorhandler

    on error resume next
    ' when the dir existe, it will generate an error, but will ignore this and
    ' resume execution at the next statement

    Dim inBackPos as Integer, inForePos as Integer
    Dim path
    inBackPos = 3
    ChDrive txtTrgPath 'Change to current drive
    ChDir "\" 'Change to current directory
    inForePos = InStr(4, txtTrgPath, "\")
    Do While inForePos <> 0
    path = mid(txtTrgPath, inBackPos + 1, inForePos - inBackPos - 1)
    MkDir path
    ChDir path
    inBackPos = inForePos
    inForePos = InStr(inBackPos + 1, txtTrgPath, "\")
    Loop





    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

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

Featured