Search Tools Links Login

Filename only from a path


Visual Basic 6, or VB Classic

Ever wanted to extract a filename only from a path including the filename, well this simple function will do it for you! (I know its not ground breaking but its simple easy and useful)

Original Author: Stewart MacFarlane

Inputs

call the function like this:
get_filename_only(filepath$) where filepath$ is a variable containing a filepat and filename
or
get_filename_only("c:windows otepad.exe") change the path to suit your needs

Assumptions

put this code into a module (.bas) and call from the program as explained above

Returns

the filename only (incl. extension) or a message telling you to check the path if it cant find a valid path in the string you sent to it.

Code

Function get_filename_only(filepath)
For x = Len(filepath) To 1 Step -1
  If Mid(filepath, x, 1) = "" Then
    get_filename_only = Right(filepath, Len(filepath) - x)
    Exit Function
  End If
Next x
get_filename_only = "Please check filepath it may be incorrect)"
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 91 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.