如何获取文件?

时间:2009-09-10 08:09:34

标签: vb6

如何仅从文件的完整路径获取文件名?

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

4 个答案:

答案 0 :(得分:4)

看看这个: Retrieve Filename without Path or Extension

以下是上述链接的功能:

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")

答案 1 :(得分:3)

这是最短的方式:

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

告诉我,如果你能找到更短的代码;)

答案 2 :(得分:0)

答案 3 :(得分:0)

-

以下是更好更短的方式。

从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