Question:
VB Question?
Ashley C
2008-05-09 18:41:39 UTC
For a minor part of a project I am opening a file in an Open File Dialog and I need to take the name of the file and just crop in down to the actual file name without the hierarchy and the data type. So far I've got this:

Dim fullName, fileName As String
Dim d, f As Integer
fullName = OFD.FileName
d = fullName.IndexOf("\")
f = fullName.IndexOf(".")
fileName = fullName.Substring(d, f - 2)
lblPlaying.Text = fileName

...but what the indexOf("\") is doing is taking it from the first "\"
I know I can probly do something like IndexOf("\", 5) but I can never know how many there will be.

Is there any way I can do this?
Three answers:
2008-05-09 18:49:39 UTC
InStrRev() returns the rightmost occurrence of the character.



But the .FileTitle property is the name (including extension) of the file, with no path. Just lop off the extension if you don't want it.



(This is VB - if you're working in VB.Net, it may or may not be the same. VB.net is not VB.)
miket
2008-05-09 23:10:43 UTC
Try these:



Function GetFileNameFromName(FullName As String) As String

Dim slash As Integer

Dim Tmp As Integer

Tmp = 1

While Tmp > 0

Tmp = InStr(Tmp + 1, FullName, "\")

If Tmp > 0 Then slash = Tmp

Wend

GetFileNameFromName = VBA.Right(FullName, Len(FullName) - slash)

End Function



that will get the name x.y from a fully-qualified name.



and



Function GetFileNameRoot(FullName As String) As String

Dim dot As Integer

Dim LastDot As Integer

dot = 1

While dot > 0

dot = InStr(dot + 1, FullName, ".")

If dot > 0 Then LastDot = dot

Wend

GetFileNameRoot = VBA.Left$(FullName, LastDot - 1)

End Function



That will remove the .type.



Sorry, man. I can see from your code that you're doing vb.net. My code is vb6 and will probably work, but I don't use any specific .net functions.
tinytim
2008-05-12 20:21:11 UTC
this will tell you how many there are: (str=filename)





MsgBox(str.Length - str.Replace("/", "").Length)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...