Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a file only?

Tags:

vb6

How to get a file name only from file's full path?

MY path -  C:\Documents and Settings\Arshad\My Documents\ravi.txt
like image 965
Gopal Avatar asked Sep 10 '09 08:09

Gopal


People also ask

How do I return only file names from the find command?

Normally find command will retrieve the filename and its path as one string. If you want to display only the filename, you can use basename command. find infa/bdm/server/source/path -type f -iname "source_fname_*. txt"

How do I get only files in Unix?

Open the command-line shell and write the 'ls” command to list only directories. The output will show only the directories but not the files. To show the list of all files and folders in a Linux system, try the “ls” command along with the flag '-a” as shown below.


2 Answers

Have a look at this: Retrieve Filename without Path or Extension.

Here's the function from the above link:

Public Function GetFileName(flname As String) As String

    'Get the filename without the path or extension.
    'Input Values:
    '   flname - path and filename of file.
    'Return Value:
    '   GetFileName - name of file without the extension.

    Dim posn As Integer, i As Integer
    Dim fName As String

    posn = 0
    'find the position of the last "\" character in filename
    For i = 1 To Len(flname)
        If (Mid(flname, i, 1) = "\") Then posn = i
    Next i

    'get filename without path
    fName = Right(flname, Len(flname) - posn)

    'get filename without extension
    posn = InStr(fName, ".")
        If posn <> 0 Then
            fName = Left(fName, posn - 1)
        End If
    GetFileName = fName
End Function

When inputting

C:\Documents and Settings\Arshad\My Documents\ravi.txt

this function returns

ravi

The function is called as such:

Dim FileName As String
FileName = GetFileName("C:\Documents and Settings\Arshad\My Documents\ravi.txt")
like image 115
bernhof Avatar answered Sep 28 '22 11:09

bernhof


This is the shortest way:

Public Function GetFileName(FilePath As String) As String
   Dim l() as string
   l=split(FilePath,"\")
   GetFileName=l(UBound(l))
End Function

Tell me, if you can find shorter code;)

like image 25
Searush Avatar answered Sep 28 '22 11:09

Searush