Public Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean = False) As Boolean
'Purpose: Return True if the file exists, even if it is hidden.
'Arguments: strFile: File name to look for. Current directory searched if no path included.
' bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
'Note: Does not look inside subdirectories for the file.
'Author: Allen Browne. [URL]http://allenbrowne.com[/URL] June, 2006.
Dim lngAttributes As Long
'Include read-only files, hidden files, system files.
lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
Else
'Strip any trailing slash, so Dir does not look inside the folder.
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
'If Dir() returns something, the file exists.
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
Public Function GetFileName(ByVal FullPath As String) As String
'**************************************************
'* Created By: Scott L Prince
'* Created On: 10/2/13
'* Modified:
'* Purpose: Returns a file name from the full path provided.
'* Parameters: Full path including file name
'* Output: File name, or empty string if no file name could be determined.
'* Comments:
'**************************************************
On Error GoTo GetFileName_Err
'Defaults
GetFileName = ""
'Only necessary if a FullPath has actually been passed.
If FullPath <> "" Then
Dim BackslashLocation As Long 'Location of the last "/" or "\" in the path
'Locate the FINAL backslash.
BackslashLocation = InStrRev(FullPath, "\")
'If no "\" was found, then check for "/" (sharepoint file structure).
If BackslashLocation = 0 Then BackslashLocation = InStrRev(FullPath, "/")
'Determine if a slash was found.
If BackslashLocation > 0 Then
'A slash was found, so return the file name.
GetFileName = Right(FullPath, Len(FullPath) - BackslashLocation)
Else
'No slash found, so return FullPath as the file name.
GetFileName = FullPath
End If
End If
GetFileName_Exit:
Exit Function
GetFileName_Err:
MsgBox "An error has occurred in procedure 'GetFileName'!" & vbCrLf & vbCrLf & _
"Error:" & vbTab & vbTab & Err.Number & vbCrLf & _
"Description:" & vbTab & Err.Description, vbOKOnly + vbCritical
Resume GetFileName_Exit
End Function
Public Function GetFolder(ByVal FullPath As String, _
Optional ByVal IncludeLastSlash As Boolean = False) As String
'**************************************************
'* Created By: Scott L Prince
'* Created On: 10/3/13
'* Modified:
'* Purpose: Returns the folder portion of the supplied path (ie - C:\Temp\Test.doc returns C:\Temp)
'* Parameters: Full path including file name
'* Output: Path to indicated folder, or empty string if no folder was found.
'* Comments:
'**************************************************
On Error GoTo GetFolder_Err
'Defaults
GetFolder = ""
'Only execute if an actual value was passed to FullPath.
If FullPath <> "" Then
Dim BackslashLocation As Long 'Location of the FINAL backslash in the path.
'Determine the location of the final backslash in the path.
BackslashLocation = InStrRev(FullPath, "\")
'If no "\" was found, then check for "/" (sharepoint file structure).
If BackslashLocation = 0 Then BackslashLocation = InStrRev(FullPath, "/")
'If there is a slash, use it to determine the path sans filename in FullPath.
If BackslashLocation > 0 Then
'If IncludeLastSlash is false, subtract 1 from Backslash location so the last one is not included.
If Not IncludeLastSlash Then BackslashLocation = BackslashLocation - 1
'Return the folder path.
GetFolder = Left(FullPath, BackslashLocation)
End If
End If
GetFolder_Exit:
Exit Function
GetFolder_Err:
MsgBox "An error has occurred in procedure 'GetFolder'!" & vbCrLf & vbCrLf & _
"Error:" & vbTab & vbTab & Err.Number & vbCrLf & _
"Description:" & vbTab & Err.Description, vbOKOnly + vbCritical
Resume GetFolder_Exit
End Function