Results 1 to 6 of 6

Thread: cutting a path

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Cool cutting a path

    i have a path

    f:\Data Team\Bureau\A G Barr Soft Drinks\ILabelClient\db.mdb

    i would like to cut it to

    f:\Data Team\Bureau\A G Barr Soft Drinks\ILabelClient\

    the database filename will be of varying lengths so i assume that i need to say "cut everything after the first '\' from the right" unfortunately i have very little idea about hwo to do that.

    any help would be most appreciated

  2. #2
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Check this CodeBank thread http://www.vbforums.com/showthread.p...hreadid=244248

    You will find a large number of useful file and folder functions here and instructions on how to use it.

    In your case, you would need the GetFilePath function

    VB Code:
    1. '****************************************************
    2. '* Function:   GetFilePath
    3. '* Parameters:
    4. '*     FilePath (String) - the full file path of the file
    5. '* Returns: The path of the file in the given file path
    6. '****************************************************
    7. Public Function GetFilePath(FilePath As String) As String
    8.     On Error GoTo GetFilePathError
    9.    
    10.     Dim slashLocation As Integer
    11.    
    12.     If Len(FilePath) = 0 Then
    13.         GetFilePath = ""
    14.         Exit Function
    15.     End If
    16.    
    17.     'Find the last \ in the path
    18.     slashLocation = InStrRev(FilePath, "\")
    19.    
    20.     If slashLocation = 0 Then
    21.         GetFilePath = ""
    22.     Else
    23.         GetFilePath = Mid$(FilePath, 1, slashLocation)
    24.     End If
    25.    
    26.     Exit Function
    27.  
    28. GetFilePathError:
    29.    
    30.     MsgBox "Error #" & Err.Number & vbCrLf & Err.Description, vbExclamation, "Error"
    31.    
    32. End Function

  3. #3
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    Re: cutting a path

    Originally posted by sagey
    i have a path

    f:\Data Team\Bureau\A G Barr Soft Drinks\ILabelClient\db.mdb

    i would like to cut it to

    f:\Data Team\Bureau\A G Barr Soft Drinks\ILabelClient\

    the database filename will be of varying lengths so i assume that i need to say "cut everything after the first '\' from the right" unfortunately i have very little idea about hwo to do that.

    any help would be most appreciated
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim l_strPath As String
    5. Dim l_strArray() As String
    6.  
    7.     Let l_strPath = "f:\Data Team\Bureau\A G Barr Soft Drinks\ILabelClient\db.mdb"
    8.     Let l_strArray = Split(l_strPath, "\")
    9.    
    10. Dim i As Integer
    11. Dim l_strFinalString
    12.  
    13.     For i = LBound(l_strArray) To UBound(l_strArray) - 1
    14.         Let l_strFinalString = l_strFinalString & l_strArray(i) & "\"
    15.     Next
    16.     MsgBox l_strFinalString
    17. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    cheers ae_jester

    how would i get the filename(and extension) in a string as well as the string holding the path
    (i need to use the file name as well in a different part of the app)

    thanks for the help so far

  5. #5
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by sagey
    cheers ae_jester

    how would i get the filename(and extension) in a string as well as the string holding the path
    (i need to use the file name as well in a different part of the app)

    thanks for the help so far
    Use the code i wrote here .... in array you will have everything ...

  6. #6
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    If you would check that link I gave you, you could have found what you were looking for.

    Here it is anyway.

    VB Code:
    1. '*******************************************************************************
    2. '** Function:   GetFileName
    3. '** Parameters:
    4. '**     FilePath (String) - the full file path of the file
    5. '** Returns: The file name of for the given file path
    6. '*******************************************************************************
    7. Public Function GetFileName(FilePath As String) As String
    8.     On Error GoTo GetFileNameError
    9.    
    10.     Dim slashLocation As Integer
    11.    
    12.     If Len(FilePath) = 0 Then
    13.         GetFileName = ""
    14.         Exit Function
    15.     End If
    16.        
    17.     'Find the last \ in the path
    18.     slashLocation = InStrRev(FilePath, "\")
    19.    
    20.     GetFileName = Mid$(FilePath, slashLocation + 1, Len(FilePath) + 1 - slashLocation)
    21.        
    22.     Exit Function
    23.  
    24. GetFileNameError:
    25.    
    26.     GetFileName = ""
    27.     MsgBox "Error #" & Err.Number & vbCrLf & Err.Description, vbExclamation, "Error"
    28.    
    29. End Function

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