codice:' GetFileName; ' Return the name of a file; ' I : strFile = full path of a file; ' O : string = file name only; ' Examples : GetFileName("c:\path\path2\file.ext") return "file.ext"; ' GetFileName("c:\path\path2") return "path2"; ' GetFileName("c:\path\path2\") return ""; ' GetFileName("file.ext") return "file.ext"; Public Function GetFileName(strFile As String) As String ' Temp splitting vector; Dim strTemp() As String ' Split the filename into a vector, using as delimiter "\"; strTemp = Split(strFile, "\") ' If the resulting vector have more than 1 element; If LBound(strTemp) <> UBound(strTemp) Then ' Return the last element (the file name); GetFileName = strTemp(UBound(strTemp)) Else ' Return the string passed as file name (we suppose that someone pass us the filename without path); GetFileName = strFile End If ' Kill the vector; Erase strTemp End Function ' GetFileExt; ' Return the extension of a file; ' I : strFile = full path of name-only of the file; ' O : string = extension or "" if file name have no extension; ' Examples : GetFileExt("c:\path\path2\file.ext") return "ext"; ' GetFileExt("file.ext") return "ext"; ' GetFileExt("c:\path\path2\file") return ""; ' GetFileExt(".ext") return "ext"; Public Function GetFileExt(strFile As String) As String ' Temp splitting vector; Dim strTemp() As String ' Split the filename into a vector, using as delimiter "\"; strTemp = Split(strFile, ".") ' If the resulting vector have more than 1 element; If LBound(strTemp) <> UBound(strTemp) Then ' Return the last element (the extension); GetFileExt = strTemp(UBound(strTemp)) Else ' No extension : return an empty string; GetFileExt = "" End If ' Kill the vector; Erase strTemp End Function ' GetFilePath; ' Return the path of a specified file; ' I : strFile = string : filename with full path; ' blnIncludeEndingSlash = boolean : if true, the returned path have an ending "\"; ' default is false; ' O : string : full path of the file; ' Examples : GetFilePath("c:\path\path2\file.ext") return "c:\path\path2"; ' GetFilePath("c:\path\path2\file.ext", True) return "c:\path\path2\"; ' Tip : you can get the previous directory with this function; ' GetFilePath("c:\path1\path2") return "c:\path1"; ' Note that GetFilePath("c:\path1\path2\") will return ' "c:\path1\path2"!; Public Function GetFilePath(strFile As String, Optional blnIncludeEndingSlash As Boolean = False) As String ' Temp splitting vector; Dim strTemp() As String ' This string will be append at the begin of the path at the end of the function; Dim strNetPath As String ' This is a simple counter; Dim i As Integer ' If the first 2 char of the given filename are "\\", then we ' got a net path; If Left(strFile, 2) = "\\" Then ' "\\" will be append at the begin of the path; strNetPath = "\\" Else ' Append nothing at the begin of the path; strNetPath = "" End If ' Split the filename using "\" as delimiter; strTemp = Split(strFile, "\") ' Reset the return path; GetFilePath = "" ' Pass all the elements of the vector (but not the last); For i = LBound(strTemp) To UBound(strTemp) - 1 ' If the resulting path is still empty do not append "\"; If GetFilePath <> "" Then GetFilePath = GetFilePath & "\" ' Append the current element of the splitting vector on the resulting path; GetFilePath = GetFilePath & strTemp(i) Next i ' Must we append the final "\"? If blnIncludeEndingSlash = True Then ' yes! GetFilePath = GetFilePath & "\" End If ' Add the eventual "\\" at the begin of the path; GetFilePath = strNetPath & GetFilePath ' Erase the splitting vector from the memory; Erase strTemp End Function

Rispondi quotando