Private Sub DevTestFileNameFunctions()
'/ this procedure for testing the file/folder name functions
Const strcDRIVE As String = "C:"
Const strcPATH1 As String = "path1"
Const strcPATH2 As String = "path2"
Const strcFNAME As String = "filename"
Const strcEXT1 As String = "ext1"
Const strcEXT2 As String = "ext2"
Const strcSEP_PATH As String = "\"
Const strcSEP_EXT As String = "."
Dim avarTestFileFolders() As Variant
Dim abytLoop As Byte
Dim strOutput As String
Dim strInput As String
ReDim avarTestFileFolders(1 To 15)
avarTestFileFolders(1) = strcDRIVE
avarTestFileFolders(2) = strcDRIVE & strcSEP_PATH
avarTestFileFolders(3) = strcDRIVE & strcSEP_PATH & strcPATH1
avarTestFileFolders(4) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH
avarTestFileFolders(5) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH & strcPATH2
avarTestFileFolders(6) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH & strcPATH2 & strcSEP_PATH
avarTestFileFolders(7) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH & strcPATH2 & strcSEP_PATH & _
strcFNAME
avarTestFileFolders(8) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH & strcPATH2 & strcSEP_PATH & _
strcFNAME & strcSEP_EXT & strcEXT1
avarTestFileFolders(9) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_PATH & strcPATH2 & strcSEP_PATH & _
strcFNAME & strcSEP_EXT & strcEXT1 & strcSEP_EXT & strcEXT2
avarTestFileFolders(10) = strcDRIVE & strcSEP_PATH & strcPATH1 & strcSEP_EXT & strcPATH2 & strcSEP_PATH & _
strcFNAME & strcSEP_EXT & strcEXT1 & strcSEP_EXT & strcEXT2
avarTestFileFolders(11) = ""
avarTestFileFolders(12) = Empty
avarTestFileFolders(13) = strcFNAME
avarTestFileFolders(14) = strcFNAME & strcSEP_EXT & strcEXT1
avarTestFileFolders(15) = strcFNAME & strcSEP_EXT & strcEXT1 & strcSEP_EXT & strcEXT2
Debug.Print Now() & vbTab & "test started"
For abytLoop = LBound(avarTestFileFolders) To UBound(avarTestFileFolders)
strOutput = vbNullString
strInput = vbNullString
strInput = (avarTestFileFolders(abytLoop))
strOutput = fnstrGetFileNameFromString(strInput, True)
'RESULTS:
'
' fnstrGetPathFromString(False)
' FAILED = 7 (FullName w/o FileExt is assumed to be a Path w/o trailing Path Sep)
'
' fnstrGetPathFromString(True)
' FAILED = 3 & 5 (Paths w/o trailing Path Sep are assumed to be a FullName w/o FileExt)
'
' fnstrGetFileNameFromString(False)
' FAILED = 7 & 13 (FileName/FullName w/o FileExt is assumed to be a Path w/o trailing Path Sep)
'
' fnstrGetFileNameFromString(True)
' FAILED = 3 & 5 (Paths w/o trailing Path Sep are assumed to be a FileName/FullName w/o FileExt)
'
' fnstrGetFileExtFromString(True or False)
' ALL PASSED!
'
Debug.Print Format(abytLoop, "00") & Space(2) & "READ: " & strOutput & Space(35 - Len(strOutput)) & _
vbTab & "FROM: " & avarTestFileFolders(abytLoop)
Next abytLoop
Debug.Print Now() & vbTab & "test completed"
End Sub
Public Function fnstrGetSeparatoredPath(ByRef strPath As String) As String
'/ doesn't detect garbage input, requires a Path arg
'/ ensures folder path ends in path separator
If Not Len(strPath) > 0 Then
Exit Function
End If
With Application
If Right$(strPath, 1) = .PathSeparator Then
fnstrGetSeparatoredPath = strPath
Else
fnstrGetSeparatoredPath = strPath & .PathSeparator
End If
End With
End Function
Public Function fnstrGetPathFromString(ByRef strFullName As String, _
Optional ByVal blnAllowFilesWithOutExts As Boolean) As String
Dim strResult As String
'remove any leading/trailing spaces
strFullName = Trim(strFullName)
Select Case Len(strFullName)
Case Is > 3
If Mid$(strFullName, 2, 2) = ":\" Then
'arg has drive prefix
ElseIf Left$(strFullName, 2) = "\\" Then
'arg is UNC
Else
'arg = FileName or garbage
GoTo ExitProcedure
End If
'therefore arg = Path or FullName
If Right$(strFullName, 1) = "\" Then
'arg = Path
strResult = strFullName
ElseIf InStr(InStrRev(strFullName, "\"), strFullName, ".") > 0 Then
'a FileExt was found after last Path Sep therefore arg = FullName
strResult = Left$(strFullName, InStrRev(strFullName, "\"))
Else
'arg = FullName (w/o Ext) or Path (w/o trailing Path Sep)
If blnAllowFilesWithOutExts Then
'the opt. bln arg set means assume arg = FullName w/o Ext
strResult = Left$(strFullName, InStrRev(strFullName, "\"))
Else
'the opt. bln arg set means assume arg = Path w/o trailing Path Sep
strResult = strFullName
End If
End If
Case 1 To 3
'the likelihood of a FileName len being less than 4 is *extremely unlikely* to occur in the Real World
'therefore the arg is almost certainly a drive (or garbage)
If Len(strFullName) > 1 Then
If Not Mid$(strFullName, 2, 1) = ":" Then
'no drive found therefore arg = garbage
GoTo ExitProcedure
Else
strResult = strFullName
End If
Else
Select Case Asc(UCase(strFullName))
Case 65 To 90
strResult = UCase(strFullName) & ":"
Case Else
'arg = garbage
End Select
End If
Case Else
'probably Null or vbnullstring
End Select
ExitProcedure:
fnstrGetPathFromString = strResult
End Function
Public Function fnstrGetFileNameFromString(ByRef strFullName As String, _
Optional ByVal blnAllowFilesWithOutExts As Boolean) As String
'/ Paths without a trailing Path Separator can be mistaken for FullNames w/o File Exts
'/ added optional boolean arg so end user can choose which one they want to err towards
Dim bytPosPathSep As Byte
'remove any leading/trailing spaces
strFullName = Trim(strFullName)
Select Case Len(strFullName)
Case Is > 3
If blnAllowFilesWithOutExts = False Then
'this optional arg has good & bad points
'good = ignores garbage input
'bad = overlooks any file missing ext
If Not InStr(strFullName, ".") > 0 Then
Exit Function
End If
End If
bytPosPathSep = InStrRev(strFullName, Application.PathSeparator)
fnstrGetFileNameFromString = Mid$(strFullName, bytPosPathSep + 1)
Case 1 To 3
'the likelihood of a filename len being less than 4 is *extremely unlikely* to occur in the real world
'this is almost certainly a drive or garbage
Case Else
'probably Null or vbnullstring
End Select
End Function
Public Function fnstrGetFileExtFromString(ByRef strFileName As String, _
Optional ByVal blnRemoveMultiExt As Boolean) As String
Dim bytPosExtSep As Byte
Dim bytPosPathSep As Byte
'remove any leading/trailing spaces
strFileName = Trim(strFileName)
If Not Len(strFileName) > 0 Then
Exit Function
End If
If blnRemoveMultiExt Then
bytPosPathSep = InStrRev(strFileName, Application.PathSeparator)
'dont detect "." in the path section
bytPosExtSep = InStr(bytPosPathSep + 1, strFileName, ".")
Else
bytPosExtSep = InStrRev(strFileName, ".", -1)
End If
If bytPosExtSep > 0 Then
fnstrGetFileExtFromString = Right$(strFileName, Len(strFileName) - bytPosExtSep)
End If
End Function
Public Function fnstrRemoveFileExtFromString(ByRef strFileName As String, _
Optional ByVal blnRemoveMultiExt As Boolean) As String
Dim strExt As String
Dim bytPos As Byte
'remove any leading/trailing spaces
strFileName = Trim(strFileName)
If Not Len(strFileName) > 0 Then
Exit Function
End If
strExt = "." & fnstrGetFileExtFromString(strFileName, blnRemoveMultiExt)
bytPos = InStrRev(strFileName, strExt)
If bytPos > 0 Then
fnstrRemoveFileExtFromString = Left$(strFileName, bytPos - 1)
Else
fnstrRemoveFileExtFromString = strFileName
End If
End Function
Public Function fnstrRemoveInvalidCharsFromFileName(ByVal strFileName As String) As String
Dim strNewString As String
strNewString = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(strFileName, "|", _
""), ">", ""), "<", ""), Chr(34), ""), "?", ""), "*", ""), ":", ""), "/", ""), "\", "")
fnstrRemoveInvalidCharsFromFileName = strNewString
End Function
Bookmarks