file name only

paulevans

Registered User.
Local time
Today, 09:50
Joined
Mar 7, 2006
Messages
79
Hi Im after a function that takes a string and finds only the file name.

So form a variable

strpath = "c:\MYDOCS\DATA\THISFILE.TXT"

i would like just the file name thisfile.txt

thanks for any help with this

PAul
 
Have you heard of INSTR ???? Once you know the position, you could use LEFT - see examples below lifted from Access' own help section.

InStr Function Example
This example uses the InStr function to return the position of the first occurrence of one string within another.

Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".

' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)

' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)

' Comparison is binary by default (last argument is omitted).
MyPos = Instr(SearchString, SearchChar) ' Returns 9.

MyPos = Instr(1, SearchString, "W") ' Returns 0.

Now LEFT

Left Function Example
This example uses the Left function to return a specified number of characters from the left side of a string.

Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr = Left(AnyString, 1) ' Returns "H".
MyStr = Left(AnyString, 7) ' Returns "Hello W".
MyStr = Left(AnyString, 20) ' Returns "Hello World".
 
Code:
Public Function LastParam(InString As String) As String

LastParam = Right(InString, Len(InString) - InStrRev(InString, "\"))

End Function
 
see this example
Code:
Public Function GetPathToFE() As String
    ' Extract Path from :-
    'V:\SYSTEMS\NOSYSTEM.MDB
    ' Reversing the string and find the first occurence of '/'
    
    Dim strReversedName As String, strName As String
    Dim strChar As String * 1
    Dim intI As Integer, intLength As Integer
    
    strName = CurrentDb.Name
    intLength = Len(strName)
    strReversedName = ""
    
    For intI = intLength To 1 Step -1
        strChar = Mid(strName, intI, 1)
        strReversedName = strReversedName & strChar
    Next

    intI = InStr(1, strReversedName, "\")
    If intI > 0 Then
        GetPathToFE = Left(strName, intLength - intI)
    Else
        GetPathToFE = strName
    End If

End Function
 
Thanks Guys

Have been playing and this was just what I wanted.

Paul
 
see this example
Code:
Public Function GetPathToFE() As String
    ' Extract Path from :-
    'V:\SYSTEMS\NOSYSTEM.MDB
    ' Reversing the string and find the first occurence of '/'
    
    Dim strReversedName As String, strName As String
    Dim strChar As String * 1
    Dim intI As Integer, intLength As Integer
    
    strName = CurrentDb.Name
    intLength = Len(strName)
    strReversedName = ""
    
    For intI = intLength To 1 Step -1
        strChar = Mid(strName, intI, 1)
        strReversedName = strReversedName & strChar
    Next

    intI = InStr(1, strReversedName, "\")
    If intI > 0 Then
        GetPathToFE = Left(strName, intLength - intI)
    Else
        GetPathToFE = strName
    End If

End Function


Dennisk:

I don't know if you knew this, but you could save yourself some steps if you use the InstrRev function (in string reverse). It will find the first instance of something starting at the end of the string and move backwards until it is located.
 
Bob,
I developed this version in Access 97 which does not have that function and I still do most of my dev in 97. But shortly we are uprading to 2003, so I may get a chance to refactor some of my algorthms.
 
Ah, that would explain it. I didn't know if you knew about that one, but you do but can't use it.

You know, I wish we had the functionality here on the website (like Utter Access does) to make users select the version they are using whn they post. It makes troubleshooting so much easier at times.
 

Users who are viewing this thread

Back
Top Bottom