Как получить только файл? - PullRequest
       9

Как получить только файл?

3 голосов
/ 10 сентября 2009

Как получить имя файла только по полному пути файла?

MY path -  C:\Documents and Settings\Arshad\My Documents\ravi.txt

Ответы [ 4 ]

4 голосов
/ 10 сентября 2009

Посмотрите на это: Получить имя файла без пути или расширения .

Вот функция по ссылке выше:

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

При вводе

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

эта функция возвращает

ravi

Функция вызывается так:

Dim FileName As String
FileName = GetFileName("C:\Documents and Settings\Arshad\My Documents\ravi.txt")
3 голосов
/ 11 октября 2012

Это кратчайший путь:

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

Скажите, можете ли вы найти более короткий код;)

0 голосов
/ 29 декабря 2014

-

Вот лучший и короткий путь.

Возвращает имя файла из FullPath

' ** C:\Windows\System32\calc.exe > Ret: calc.exe
Private Function GetFilenameFromPath(FullPath As String) As String
   GetFilenameFromPath = Right(FullPath, Len(FullPath) - InStrRev(FullPath, "\"))
End Function

Путь возврата из FullPath

' C:\Windows\System32\calc.exe > Ret: C:\Windows\System32\
Private Function GetDirectoryFromPath(FullPath As String) As String
   GetDirectoryFromPath = Left(FullPath, InStrRev(FullPath, "\"))
End Function
0 голосов
/ 10 сентября 2009
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...