Instr

aziz rasul

Active member
Local time
Today, 22:19
Joined
Jun 26, 2000
Messages
1,935
I have a string which follows the following format: -

C:\My Documents\Text Files\Samples\textfilename.txt

How do I use the InStr function so that I obtain the positions of each "\" value?

[This message has been edited by aziz rasul (edited 05-22-2001).]
 
You're going to need a custom function if you want the position of each and every instance.

As there may be a variable number of occurences of the character (or none at all) in any given string, the question arises how to pass the data regarding the various positions back to the program in a useful format, probably an array would be the way to do it.

However, I suspect that you might just want to know the position of the last '\' character so that you can get the filename on it's own, am I right?, Here's a function (actually two functions) that will do it:

Code:
Public Function reverse(yourstring As String)
Dim intLoop As Integer
For intLoop = Len(yourstring) To 1 Step -1
    reverse = reverse & Mid(yourstring, intLoop, 1)
Next intLoop

Public Function GetFileName(YourFileAndPath As String) As String
GetFileName = Right(YourFileAndPath, (InStr(reverse(YourFileAndPath), "\") - 1))
End Function

so:

GetFileName(":\My Documents\Text Files\Samples\textfilename.txt")

returns

"textfilename.txt"

HTH

Mike
 
Thanks for that. I had already worked out how to obtain the position of the first and last "\". However your code is better than what I had. I did want to know the position of the last "\". However I knew the question would introduce the use of arrays and I wanted to see how this could be done.

Out of curiosity what does the HTH mean. I have seen this in other people's messages and can't guess what it might mean. Also some people use RTH as well?
 

Users who are viewing this thread

Back
Top Bottom