Truncate last five characters

  • Thread starter Thread starter Jesse Hamilton
  • Start date Start date
J

Jesse Hamilton

How would you code to truncate the .hml or .html from the tail end of a
filename string?

Jesse
 
Jesse

from a reply by Tom Ogilvy to a similar question (slightly modified):

sfilename = Left(sfilename,len(sFilename)-5)

Regards

Trevor
 
You can use the left function for example

Sub test()
Dim fname As String
fname = "C:\testfile.htm"
MsgBox fname
MsgBox Left(fname, Len(fname) - 4)
End Sub
 
Maybe something like:

If Instr(sFilename,".htm") > 0 then
If Right(sFilename,5) = ".html" then
sFilename = Left(sFilename,len(sFilename)-5)
else
sFilename = Left(sFilename,len(sFilename)-4)
End if
End if
 
Great thought. Since you are using Instr


If instr(sfilename,".htm") Then ' any non-zero number is TRUE
sfilename = Left(sfilename,instr(sfilename,".htm")-1)
End if
 
That's clever :)

--
Darren
Tom Ogilvy said:
Great thought. Since you are using Instr


If instr(sfilename,".htm") Then ' any non-zero number is TRUE
sfilename = Left(sfilename,instr(sfilename,".htm")-1)
End if
 
The previous solutions all suffer from assumptions that the file
extension is X characters long (e.g. 3 or 4), or is such-and-such
character string (e.g. "htm"). Those kinds of assumptions will cause
debugging later on when reality intrudes. ;-)

Here's a more generic way to do this that will work with file
extensions of any length, no length, or containing multiple periods
(hence the StrReverse). It's coded as a function so it can be called
where ever needed w/o rewriting the code. If you don't know how to
integrate the function into your code you can just take the code
inside the function (wow, that sounded weird!).

________________________________________________________________

Sub joe()
Dim sFilename As String ' original filename

sFilename = "Hello World"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))

sFilename = "Hello World.htm"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))

sFilename = "Hello World.html"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))

sFilename = "Hello World.Peace.doc"
MsgBox (sFilename & vbCrLf & StripExtension(sFilename))

End Sub
________________________________________________________________

Function StripExtension(Filename As String) As String

Dim sEmanelif As String ' reversed filename
Dim sFileNoExt As String ' filename w/o extension

If InStr(1, Filename, ".") = 0 Then
StripExtension = Filename
Else
' reverse Filename string to get extension at front
sEmanelif = StrReverse(Filename)
' take from 1 past period onward
sEmanelif = Mid(sEmanelif, InStr(sEmanelif, ".") + 1)
' reverse back to original and place in function result
StripExtension = StrReverse(sEmanelif)
End If

End Function
 
No, the previous posts answered the question asked and didn't assume xl2000
like you are assuming (which could cause problems later when reality
intrudes).
How would you code to truncate the .hml or .html from the tail end of a
filename string?
 
My apologies to all, that did come across as condescending. I was
trying to show Jesse that the more you go from a specific,
"works-only-if", solution to generic solution, the better. And if you
look at the flow of the answers, they were going that way anyway. :-)
 
Back
Top