Results 1 to 6 of 6

Thread: How to extract a directory name?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    38

    How to extract a directory name?

    How will i extract a directory name? Like for example:

    c:\temp\alvin\alvin.txt

    I would just like to have c:\temp\alvin\

  2. #2
    Lively Member
    Join Date
    Oct 2002
    Location
    behind a pc
    Posts
    117
    you could jus try
    VB Code:
    1. Curdir

    If that doesn't work you'd have to search the string:

    VB Code:
    1. dim endpt as integer 'endpt of the pathstring
    2. dim startpt as integer 'startpt of the pathstring
    3. dim path as string 'the string with the path
    4. dim intpath as integer 'length of the string
    5. dim strpath as string 'the actual path
    6.  
    7. endpt=len(path) 'length of the string
    8. startpt=0 'if this bugs then try starting at 1. have had this error
    9.  
    10. for i = 0 to endpt 'walk through the entire string
    11.   startpt = InStr(startpt, Path, "\", vbTextCompare)
    12.  
    13.   if startpt <> 0 then 'if nothing is found the startpt is 0 else it will
    14.   'increment and stop at 0
    15.      intPath = startpt 'intpath is the length
    16.   end if
    17.  
    18. next i
    19.  
    20. strPath = Mid(strPath, 0, intPath) ' grab the path

    Now it might not work 100% but i think you can take it from here
    Regards,

    danielsan

    'Even though the book is finished. There's no need to close and forget.'

  3. #3
    Lively Member
    Join Date
    Jun 2001
    Location
    Viet Nam
    Posts
    98
    I think this code is shorter
    Code:
    Dim nPos as long
    nPos = InstrRev(strPath,"\")
    If nPos<>0 then
    strPath = Left(strPath,nPos)
    EndIF

  4. #4
    Lively Member
    Join Date
    Oct 2002
    Location
    behind a pc
    Posts
    117
    absolutely right, now just have to execute that number of times to make sure it is the last \ right?
    Regards,

    danielsan

    'Even though the book is finished. There's no need to close and forget.'

  5. #5
    Hyperactive Member goatsucker's Avatar
    Join Date
    Dec 2002
    Location
    Leeds, England
    Posts
    283
    You should only have to use InstrRev once as this function searchs from the end of the string. huyhk's code will work fine as it is.
    After all "Rust Never Sleeps"

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    38
    i will try all your suggestions, thanks a lot!

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