
Getting filename out of fully qualified path
Everyone tells you to use Dir(~~) functions... not much use if you are
coding a 'Save' operation, and the file (or even the target directory)
don't exist yet!
TAke a look at these three string-handling functions. Apologies for the
Variable-naming convention - and for the fact that they are heavily
'coupled' - you need all three of them, or they don't work:
Function funcDirFromFullPath(ByVal strFullName As String) As String
On Error GoTo ERRfuncDirFromFullPath
'Returns the dir part of a full dos path
'Dir(MyString) doesn't work if file strFullName doesn't exist yet...
as in SaveAs operations
funcDirFromFullPath = ""
funcDirFromFullPath = Left(strFullName, Len(strFullName) - Len
(funcFileFromFullPath(strFullName)))
EXITfuncDirFromFullPath:
Exit Function
ERRfuncDirFromFullPath:
funcDirFromFullPath = ""
Resume EXITfuncDirFromFullPath
End Function
Function funcFileFromFullPath(ByVal strFullName As String) As String
On Error GoTo ERRfuncFileFromFullPath
'Returns the filename part of a full dos path
'Dir(MyString) doesn't work if file strFullName doesn't exist yet...
as in SaveAs operations
funcFileFromFullPath = ""
'extract file name by string manipulation... find the last
occurrence of backslash
strFullName = funcReverseString(strFullName)
strFullName = Left(strFullName, InStr(strFullName, "\") - 1)
funcFileFromFullPath = funcReverseString(strFullName)
'strDataSaveDir = Left(strDataSaveFullName, Len
(strDataSaveFullName) - Len(strDataSaveFile) - 1)
EXITfuncFileFromFullPath:
Exit Function
ERRfuncFileFromFullPath:
funcFileFromFullPath = ""
Resume EXITfuncFileFromFullPath
End Function
Function funcReverseString(ByVal MyString As String) As String
On Error GoTo ERRfuncReverseString
Dim StringReversed As String
Dim MyStringLength As Integer
Dim i As Integer
funcReverseString = ""
MyStringLength = Len(MyString)
For i = 1 To MyStringLength
StringReversed = Mid(MyString, i, 1) & StringReversed
Next i
funcReverseString = StringReversed
EXITfuncReverseString:
Exit Function
ERRfuncReverseString:
funcReverseString = ""
Resume EXITfuncReverseString
End Function
Regards - Nigel Heffernan
For Demonstration purposes only. Code is provided 'As-Is' entirely
without warranty and no liability is accepted for any loss or damage
arising from its use, howsoever caused. In incorporating this code into
a live application you undertake full responsibility for testing and
all liabilities arising from its use in that application.
Quote:
> How do get the 'path' and 'filename' parts of a fully qualified path.
> Ex.
> For c:\data\access\file.mdb
> I would like a function that returns:
> c:\data\access\
> And another that returns:
> file.mdb
> Is there an easy way to do this or do I have to write my own code?
> --
> Please respond to the newsgroup AND the author:
> Thanks
> Shawn
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.