PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Parsing The Path {RESOLVED}-VBForums
Results 1 to 8 of 8

Thread: Parsing The Path {RESOLVED}

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Resolved Parsing The Path {RESOLVED}

    If I have a path like: c:\folder1\folder2\folder3\myfile.txt I know that I can parse this list using SPLIT to retrieve the file name.

    What I can't seem to figure out is how to retrieve just the path. How would I put c:\folder1\folder2\folder3\ in a string variable?
    Last edited by SeanK; Feb 22nd, 2005 at 01:27 PM.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Parsing The Path

    Something like the following may work for you:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strPath$, pos%
    3.  
    4.     strPath = "c:\folder1\folder2\folder3\myfile.txt"
    5.     pos = InStrRev("c:\folder1\folder2\folder3\myfile.txt", "\")
    6.     Debug.Print "Path: " & Left(strPath, pos - 1)
    7.     Debug.Print "File: " & Mid(strPath, pos + 1)
    8.  
    9. End Sub

  3. #3

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Parsing The Path

    Thank you sir!

    If I had a dime for everything I've said (or thought): Rhinobull, you are the MAN! I'd never have to work again!!
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  4. #4

  5. #5
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Parsing The Path {RESOLVED}

    How would one adapt this so as to extract other parts of the path?
    If I wanted to pull folder1 and folder3 from the above path and assign them to string variables, can InStrRev be used to find not the last occurence of "\", but the second or third?

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Parsing The Path {RESOLVED}

    There are many ways to parse string so here is just a one of them but kim that under circumstances it might not work:
    VB Code:
    1. Private Sub Command1_Click()
    2. '======================================
    3. Dim strPath$, pos%, i%
    4. Dim strFolders$, arFolders() As String
    5.  
    6.     strPath = "c:\folder1\folder2\folder3\myfile.txt"
    7.     pos = InStrRev("c:\folder1\folder2\folder3\myfile.txt", "\")
    8.    
    9.     strFolders = Left(strPath, pos - 1)
    10.     pos = InStr(1, strFolders, ":\")
    11.     If pos > 0 Then
    12.         strFolders = Mid(strFolders, pos + 1)
    13.     End If
    14.     strFolders = Replace(strFolders, "\\", "")
    15.    
    16.     arFolders() = Split(strFolders, "\")
    17.    
    18.     For i = 0 To UBound(arFolders)
    19.         Debug.Print arFolders(i)
    20.     Next i
    21.  
    22. End Sub

  7. #7
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Parsing The Path {RESOLVED}

    Have a look at my URL/Path string functions Codebank post, the link is in my sig. A lot of these sort of functions, including one to do your original task, that you might find useful there, and you could adapt them to do other things

  8. #8
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Re: Parsing The Path {RESOLVED}

    VB Code:
    1. Dim fso As Object     'FileSystemObject
    2.  
    3.     Dim sPath As String
    4.     Dim sParsedPath As String
    5.     Dim sBaseFilename As String
    6.     Dim sExtension As String
    7.     Dim sDrive As String
    8.    
    9.     ' your path and file name
    10.     sPath = "c:\folder1\folder2\folder3\myfile.txt "
    11.    
    12.    
    13.     ' set object reference
    14.     Set fso = CreateObject("Scripting.FileSystemObject") 'New FileSystemObject
    15.    
    16.     ' path
    17.     sParsedPath = fso.GetParentFolderName(sPath)
    18.    
    19.     ' filename minus path and extension
    20.     sBaseFilename = fso.GetBaseName(sPath)
    21.    
    22.     ' extension of file
    23.     sExtension = fso.GetExtensionName(sPath)
    24.    
    25.     ' drive letter
    26.     sDrive = fso.GetDrive(fso.GetDriveName(sPath))
    27.    
    28.  
    29.     ' destroy reference
    30.     Set fso = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width