2009-09-10 7 views

Répondre

4

Jetez un oeil à ceci: Retrieve Filename without Path or Extension.

est ici la fonction à partir du lien ci-dessus:

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 

Lors de l'entrée

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

cette fonction retourne

ravi

La fonction est appelée en tant que tel:

Dim FileName As String 
FileName = GetFileName("C:\Documents and Settings\Arshad\My Documents\ravi.txt") 
+0

@Bernhof - je besoin RAVI que je – Gopal

+0

mis à jour le poste. L'article auquel je suis lié en premier lieu, a un extrait de code qui fait exactement cela. – bernhof

+0

Très, très gros code! – Searush

3

Ceci est le chemin le plus court:

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

Dites-moi , si vous pouvez trouver le code plus court;)

0

-

Voici la meilleure façon et court.

nom du fichier de retour de 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 

chemin de retour de 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 
Questions connexes