Extracting Path / Filename from a String
Posted
by Chris Abbott
on February 7th, 2004
option Explicit
private Type PathInfo
FileNamewExt as string
FileNamewoExt as string
FileExt as string
FilePath as string
FileIsValid as Boolean
End Type
private Sub ExtractInfo(Source as string, _
ReturnInfo as PathInfo)
Dim intCurrentPos as Integer, intFileNamePos as Integer
Dim intFileExtPos as Integer
'clear UDT passed in if there is any existing data
ReturnInfo.FileExt = ""
ReturnInfo.FileIsValid = false
ReturnInfo.FileNamewExt = ""
ReturnInfo.FileNamewoExt = ""
ReturnInfo.FilePath = ""
If Source <> "" then
'get file name by finding the last slash in the string
Do
intCurrentPos = InStr(intCurrentPos + 1, Source, "\", _
vbTextCompare)
If intCurrentPos <> 0 then
intFileNamePos = intCurrentPos
else
intFileNamePos = intFileNamePos + 1
Exit Do
End If
Loop
'get file extension position if any
intFileExtPos = InStr(intFileNamePos, Source, ".", _
vbTextCompare) + 1
If intFileExtPos = 1 then
Exit Sub
End If
'put data in UDT passed in
If intFileNamePos <> 1 then
ReturnInfo.FileNamewExt = mid(Source, intFileNamePos, _
len(Source) - intFileNamePos + 1)
ReturnInfo.FilePath = mid(Source, 1, _
intFileNamePos - 2)
End If
If intFileNamePos <> 1 And intFileExtPos <> 1 then
ReturnInfo.FileNamewoExt = mid(Source, intFileNamePos, _
intFileExtPos - _
intFileNamePos - 1)
ReturnInfo.FileExt = mid(Source, intFileExtPos, _
len(Source) - intFileExtPos + 1)
End If
If Dir(Source, vbArchive + vbHidden + vbNormal + _
vbReadOnly + vbSystem) <> "" then
ReturnInfo.FileIsValid = true
else
ReturnInfo.FileIsValid = false
End If
End If
End Sub

Comments
Good Code
Posted by Legacy on 06/18/2002 12:00amOriginally posted by: Rodrigo Silva
Thanks to the Net that I found this solution: I know I could not be the first in having the problem that you solved
ReplyPlease send me a solution
Posted by Legacy on 05/15/2002 12:00amOriginally posted by: Manojkumar Vattakkat
Dear friend
If any one of you are having an .OCX/Function for JUSTIFY A TEXT BOX in VB (JUSTIFY means both
left and right side of paragraph should be aligned properly. you can confirm it with typing a paragraph in
MSWORD and press JUSTIFY button of FORMATTING tool bar after selecting the para.) , Kindly send me.
It will be a greate help for me for doing some works in Visual basic 6.
Regards
Manojkumar Vattakkatt
ReplyTry this approach
Posted by Legacy on 07/27/2001 12:00amOriginally posted by: Sameeg Kader
ReplyExtracting Path / Filename from a String
Posted by Legacy on 07/24/2001 12:00amOriginally posted by: v.udai kumar
ReplyAnother bug
Posted by Legacy on 05/18/2001 12:00amOriginally posted by: Paul Groot
This code also fails if the filename contains more than one dot (like "c:\temp\somefile.doc.zip").
A more reliable aproach is to use FileSystemObject:
Function GetTheBase(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheBase = fso.GetBaseName(filespec)
End Function
ReplyExtracting Path / Filename from a String
Posted by Legacy on 12/15/1999 12:00amOriginally posted by: Jorge Patr�o
Reply