Re: Can this be written better?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> wrote in message
news:%23SrcG13cHHA.4720@xxxxxxxxxxxxxxxxxxxxxxx

Actually, having given it a little more thought, it appears that the space
character (which my posted code properly accounts for) is the only
character, apart from the dot itself, which is permitted in a file name but
not permitted in a file extension. So, my previously posted code to strip
off a file extension is fine as it stands. It does not check the filename
itself for validity because it assumes the string you are passing to it is
the name of a file that actually exists on your disk. I'm sure there will be
snags with it that I haven't thought of, and Rick will probably turn it into
one of his famous "one liners", but here it is again, this time presented as
a function:

Mike

Private Function stripExtension(s1 As String) As String
Dim p1 As Long, p2 As Long
p1 = InStrRev(s1, ".")
p2 = InStrRev(s1, Space$(1))
If p1 > 0 And p2 < p1 Then
s1 = Left$(s1, p1 - 1)
End If
stripExtension = s1
End Function



.



Relevant Pages

  • Re: ExtractFileExt() or OpenDialog1.FileName Issue
    ... I am analysisng the file extension so that if it is .mp3 it will play ... Now the problem with this is it demands a PAnsiChar to work, ... this to a string, and then putting the string in the the function. ... Function is given by the OpenDialog Filename property: ...
    (comp.lang.pascal.delphi.misc)
  • Re: Can this be written better?
    ... VB has various functions which will search for a substring inside a string and return its position for you. ... There are also functions that will return a specified number of characters of a string for you, from either the left side, right side or somewhere in the middle. ... if you want to remove the file extension no matter what it is then you need to do it differently and you also need to take into account that on modern versions of Windows a file extension can have more than three characters. ...
    (microsoft.public.vb.general.discussion)
  • Re: convenient C/C++ compiler
    ... Are there any C implementations where file extension is meaningless? ... The first character shall be a letter. ...
    (comp.lang.c)
  • Validation of string extension and editing documents
    ... I have look at this site for some time now getting tidbits of ... *S* I have a task where I am trying to validate a file extension to ... make sure it is valid.The extension would either be xls, doc, text, ... String TITLE=""; ...
    (comp.lang.java.programmer)
  • RE: Change File Extension before importing
    ... Dim strOldName as String ... Dave Hargis, Microsoft Access MVP ... wondering if there's some code that would change the file extension before ...
    (microsoft.public.access.externaldata)