Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting filename from filepath 1

Status
Not open for further replies.

Jacquesvdz

Programmer
Jul 2, 2003
9
ZA
If I have a Filepath
strFilePath = C:\HORIZON DEV\HorizonPackage\Development\Horizon\HORIZON Debug.VBG

How do I get the FilePath like this: C:\HORIZON DEV\HorizonPackage\Development\Horizon\..
without the FileName.vbg

and get the FileName alone
strFileName = HORIZON Debug.VBG

So on the end it I must get this
StrFilePath = C:\HORIZON DEV\HorizonPackage\Development\Horizon

strFileName = HORIZON Debug.VBG

from the original

strFilePath = C:\HORIZON DEV\HorizonPackage\Development\Horizon\HORIZON Debug.VBG

Keep in mind the URL will always differ.So it has to be the Filename after the last "\"


Any Idea how to just split fileName from the path

 
I know this might be long winded, but I works for me

C = 0 'this is used as a counter
For x = Len(strFileName) To 1 Step -1
If Mid$(strFileName, x, 1) = "\" Then
strFilePath = Right$(strFileName, C)
strFileName = Left$(strFileName, (Len(strFileName) - C))
Exit For
End If
C = C + 1
Next x
 
If you just want to extract the path/filename from a string you coulod use the split command.

i.e.
Code:
Dim strOriginal As String
Dim strSplit() As String
Dim strPath As String
Dim strFilename As String
Dim i

strOriginal = "C:\HORIZON DEV\HorizonPackage\Development\Horizon\HORIZON Debug.VBG"
strSplit = Split(strOriginal, "\")

strFilename = strSplit(UBound(strSplit))
For i = 0 To (UBound(strSplit) - 1)
    strPath = strPath & strSplit(i) & "\"
Next

MsgBox strPath
MsgBox strFilename

If you are using FSO to get the path then there are options to simply select the filename and path.

 
strFilePath = C:\HORIZON DEV\HorizonPackage\Development\Horizon\HORIZON Debug.VBG

len(strfilepath) will give 67
InStrRev(strfilepath,"\")
will return the first positon of "\" as 17
use mid function to extract location and filename

mid(strfilepath,1,(67-17)) path name
and mid(strfilepath,50,17) as filename

regards
John Philip

*** Even the Best, did the Bad and Made the Best ***

John Philip
 
personally I use the GetParentFolderName method of the filesystem object...

Code:
dim objFSO as filesystemobject
set objFSO = new filesystemobject

debug.print "Path = "; objfso.GetParentFoilderName("c:\Program Files\Microsoft Office\word.exe")

etc

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
i have used ca8msm method
It works exellent.
All that was needed is to set the strFilePath = ""
before the loop.
Thanx guys for all your reply's
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top