ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Extract Path From String (https://www.excelbanter.com/excel-programming/331860-extract-path-string.html)

Andibevan[_2_]

Extract Path From String
 
Hi All,

If I have the following string assigned to var_full "C:\my documents\test
folder\test doc.doc"

What is the best way to extract the path from the above string so that the
output is "C:\my documents\test folder"

The output string needs to NOT finish in a backslash.

The solution also needs to be able to cope with any number of folder
hierarchies.

Any ideas?

Ta

Andi



Dave Peterson[_5_]

Extract Path From String
 
What version of excel are you using?

xl2k+

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath as string

myStr = "C:\my documents\test folder\test doc.doc"

myPath = Left(myStr, InStrRev(myStr, "\") - 1)

MsgBox myPath

End Sub

before xl2k:

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath As String
Dim iCtr As Long

myStr = "C:\my documents\test folder\test doc.doc"

For iCtr = Len(myStr) To 1 Step -1
If Mid(myStr, iCtr, 1) = "\" Then
myPath = Left(myStr, iCtr - 1)
Exit For
End If
Next iCtr

MsgBox myPath
End Sub

Andibevan wrote:

Hi All,

If I have the following string assigned to var_full "C:\my documents\test
folder\test doc.doc"

What is the best way to extract the path from the above string so that the
output is "C:\my documents\test folder"

The output string needs to NOT finish in a backslash.

The solution also needs to be able to cope with any number of folder
hierarchies.

Any ideas?

Ta

Andi


--

Dave Peterson

Andibevan[_3_]

Extract Path From String
 
It needs to be compatable with at least XL2000.

"Dave Peterson" wrote in message
...
What version of excel are you using?

xl2k+

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath as string

myStr = "C:\my documents\test folder\test doc.doc"

myPath = Left(myStr, InStrRev(myStr, "\") - 1)

MsgBox myPath

End Sub

before xl2k:

Option Explicit
Sub testme()

Dim myStr As String
Dim myPath As String
Dim iCtr As Long

myStr = "C:\my documents\test folder\test doc.doc"

For iCtr = Len(myStr) To 1 Step -1
If Mid(myStr, iCtr, 1) = "\" Then
myPath = Left(myStr, iCtr - 1)
Exit For
End If
Next iCtr

MsgBox myPath
End Sub

Andibevan wrote:

Hi All,

If I have the following string assigned to var_full "C:\my documents\test
folder\test doc.doc"

What is the best way to extract the path from the above string so that
the
output is "C:\my documents\test folder"

The output string needs to NOT finish in a backslash.

The solution also needs to be able to cope with any number of folder
hierarchies.

Any ideas?

Ta

Andi


--

Dave Peterson





All times are GMT +1. The time now is 04:35 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com