ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Function to return filepath given full filename (https://www.excelbanter.com/excel-programming/293116-function-return-filepath-given-full-filename.html)

Matt Lawson[_3_]

Function to return filepath given full filename
 
I need a function (whether built in or user defined) that can return the filepath (ie C:\mypath ) given the full filename with path ( as returned in the GetOpenFileName function, ie C:\mypath\myfile.xls )

Any help would be greatly appreciated

Matt Lawso



Matt Lawson[_3_]

Function to return filepath given full filename
 
ANy suggestions to improve this function

(I thought about it for a while and came up with a solution

Function ReturnPath(sFname
Dim spath As String, i As Long, leng As Intege
leng = Len(sFname
For i = 1 To leng -
If Mid(sFname, leng - i, 1) = "\" The
spath = Mid(sFname, 1, leng - i - 1
Exit Fo
End I
Nex

ReturnPath = spat

End Function

Tom Ogilvy

Function to return filepath given full filename
 
Public Function sPath(sStr As String)
If InStr(sStr, "\") = 0 Then
sPath = ""
Else
i = Len(sStr)
Do While i 0
If Mid(sStr, i, 1) = "\" Then
sPath = Left(sStr, i )
Exit Do
End If
i = i - 1
Loop
End If
End Function


If you have excel 2000 or later, you can use split to get the filename
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = (sStr,"\")
sPath = Left(sStr,len(sStr)-v(ubound(v)))

--
Regards,
Tom Ogilvy




"Matt Lawson" wrote in message
...
I need a function (whether built in or user defined) that can return the

filepath (ie C:\mypath ) given the full filename with path ( as returned in
the GetOpenFileName function, ie C:\mypath\myfile.xls ).

Any help would be greatly appreciated,

Matt Lawson





Vasant Nanavati

Function to return filepath given full filename
 
Try:

Left(FullFileName, Len(FullFileName)-Len(Dir(FullFileName)) - 1)

Assumes this is an existing file on the user's computer.

(Credit to Ivan F. Moala!)


--

Vasant





"Matt Lawson" wrote in message
...
I need a function (whether built in or user defined) that can return the

filepath (ie C:\mypath ) given the full filename with path ( as returned in
the GetOpenFileName function, ie C:\mypath\myfile.xls ).

Any help would be greatly appreciated,

Matt Lawson





Harlan Grove

Function to return filepath given full filename
 
"Tom Ogilvy" wrote...
....
If you have excel 2000 or later, you can use split to get the filename
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = (sStr,"\")
sPath = Left(sStr,len(sStr)-v(ubound(v)))

....

If OP has XL2K or later, OP could use InStrRev.

p = InStrRev(sStr, "\")
sPath = IIf(p 0, Mid(sStr, p + 1), "")



Dave Peterson[_3_]

Function to return filepath given full filename
 
Just a correction of typos:

Dim v As Variant
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = Split(sStr, "\")
sPath = Left(sStr, Len(sStr) - Len(v(UBound(v))))
'MsgBox sPath



Tom Ogilvy wrote:

Public Function sPath(sStr As String)
If InStr(sStr, "\") = 0 Then
sPath = ""
Else
i = Len(sStr)
Do While i 0
If Mid(sStr, i, 1) = "\" Then
sPath = Left(sStr, i )
Exit Do
End If
i = i - 1
Loop
End If
End Function

If you have excel 2000 or later, you can use split to get the filename
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = (sStr,"\")
sPath = Left(sStr,len(sStr)-v(ubound(v)))

--
Regards,
Tom Ogilvy

"Matt Lawson" wrote in message
...
I need a function (whether built in or user defined) that can return the

filepath (ie C:\mypath ) given the full filename with path ( as returned in
the GetOpenFileName function, ie C:\mypath\myfile.xls ).

Any help would be greatly appreciated,

Matt Lawson



--

Dave Peterson


Tom Ogilvy

Function to return filepath given full filename
 
Bunch of typos on that - guess I shouldn't attempt an xl2000 solution if I
only have xl97 on that machine <g. Thanks for the correction.

--
Regards,
Tom Ogilvy

"Dave Peterson" wrote in message
...
Just a correction of typos:

Dim v As Variant
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = Split(sStr, "\")
sPath = Left(sStr, Len(sStr) - Len(v(UBound(v))))
'MsgBox sPath



Tom Ogilvy wrote:

Public Function sPath(sStr As String)
If InStr(sStr, "\") = 0 Then
sPath = ""
Else
i = Len(sStr)
Do While i 0
If Mid(sStr, i, 1) = "\" Then
sPath = Left(sStr, i )
Exit Do
End If
i = i - 1
Loop
End If
End Function

If you have excel 2000 or later, you can use split to get the filename
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = (sStr,"\")
sPath = Left(sStr,len(sStr)-v(ubound(v)))

--
Regards,
Tom Ogilvy

"Matt Lawson" wrote in message
...
I need a function (whether built in or user defined) that can return

the
filepath (ie C:\mypath ) given the full filename with path ( as returned

in
the GetOpenFileName function, ie C:\mypath\myfile.xls ).

Any help would be greatly appreciated,

Matt Lawson



--

Dave Peterson




Tom Ogilvy

Function to return filepath given full filename
 
Excellent suggestion - but as written that gives the filename. For the path
I think you meant:

p = InStrRev(sStr, "\")
sPath = IIf(p 0, Left(sStr, p), "")


--
Regards,
Tom Ogilvy

"Harlan Grove" wrote in message
...
"Tom Ogilvy" wrote...
...
If you have excel 2000 or later, you can use split to get the filename
sStr = "C:\Myfolder1\myfolder2\myfile.xls"
v = (sStr,"\")
sPath = Left(sStr,len(sStr)-v(ubound(v)))

...

If OP has XL2K or later, OP could use InStrRev.

p = InStrRev(sStr, "\")
sPath = IIf(p 0, Mid(sStr, p + 1), "")





Dave Peterson[_3_]

Function to return filepath given full filename
 
Doesn't stop me for posting xl97 solutions, er, suggestions with only xl2002
available <vvbg.

Tom Ogilvy wrote:

Bunch of typos on that - guess I shouldn't attempt an xl2000 solution if I
only have xl97 on that machine <g. Thanks for the correction.

--
Regards,
Tom Ogilvy


<<snipped


--

Dave Peterson



All times are GMT +1. The time now is 12:24 PM.

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