DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 4 123 ... LastLast
Results 1 to 15 of 55

Thread: Determining a file extension

Hybrid View

  1. #1
    Mike Kraemer Guest

    Determining a file extension


    Hi Everyone!
    I'm building a routine that builds the path to image files that are retreive
    for user reviews. The files may be either file.jpg or file.tif. Any recomendations
    as to which call if any to use?
    Thanks in advance for your assistance.

    Mike

  2. #2
    Karl E. Peterson Guest

    Re: Determining a file extension

    Hi Mike --

    > I'm building a routine that builds the path to image files that are retreive
    > for user reviews. The files may be either file.jpg or file.tif. Any recomendations
    > as to which call if any to use?


    I'm not sure your question here quite equates to the Subject above, so I'll assume
    the Subject was the more succinct way of asking?

    To get the extension, you can use InstrRev to find the position of the last dot in
    the filename, and read what follows:

    nDot = InstrRev(filename$, ".")
    If nDot < Len(filename$) And nDot > 0 Then
    Ext$ = Mid$(filename$, nDot + 1)
    End If

    If you're using VB5, or would like to write more portable code, use a reverse loop
    through the string:

    For i = Len(filename$) To 1 Step -1
    If Mid$(filename$, i, 1) = "." Then
    If i < Len(filename$) Then
    Ext$ = Mid$(filename$, i + 1)
    End If
    Exit For
    End If
    Next i

    That what you're after?

    Later... Karl
    --
    http://www.mvps.org/vb



  3. #3
    Rob Teixeira Guest

    Re: Determining a file extension


    I'm not exactly sure what the real question is either.
    However, if you need a file/path utility, just look to shlwapi.dll.
    This DLL is present on Win98 and higher (win95 if IE4.0 is present).
    It has roughly 75 utility functions that let you deal with file and path
    names, including URL's, which allows you to strip parts, find matches, add,
    rename, concatinate, remove backslashes, etc.

    If you want to know if a given file has a certain extension, you can use
    the PathMatchSpec function.

    Declare Function PathMatchSpec Lib "shlwapi.dll" _
    alias "PathMatchSpecA" (ByVal pszFileParam As String, _
    ByVal pszSpec As String) As Long

    . . .

    Function FileIsJPG (strFileName As String) As Boolean
    Dim lngRetVal As Long

    lngRetVal = PathMatchSpec(strFileName, "*.jpg")
    FileIsJPG = CBool(lngRetVal)
    End Function


    Hope that helps.
    -Rob



    "Karl E. Peterson" <karl@mvps.org> wrote:
    >Hi Mike --
    >
    >> I'm building a routine that builds the path to image files that are retreive
    >> for user reviews. The files may be either file.jpg or file.tif. Any recomendations
    >> as to which call if any to use?

    >
    >I'm not sure your question here quite equates to the Subject above, so I'll

    assume
    >the Subject was the more succinct way of asking?
    >
    >To get the extension, you can use InstrRev to find the position of the last

    dot in
    >the filename, and read what follows:
    >
    > nDot = InstrRev(filename$, ".")
    > If nDot < Len(filename$) And nDot > 0 Then
    > Ext$ = Mid$(filename$, nDot + 1)
    > End If
    >
    >If you're using VB5, or would like to write more portable code, use a reverse

    loop
    >through the string:
    >
    > For i = Len(filename$) To 1 Step -1
    > If Mid$(filename$, i, 1) = "." Then
    > If i < Len(filename$) Then
    > Ext$ = Mid$(filename$, i + 1)
    > End If
    > Exit For
    > End If
    > Next i
    >
    >That what you're after?
    >
    >Later... Karl
    >--
    >http://www.mvps.org/vb
    >
    >



  4. #4
    Mike Kraemer Guest

    Re: Determining a file extension


    Hi Karl and Rob!
    Thanks!, Rob that was exactly what it took! Where can I learn more about
    the shlwapi.dll? Thanks again.

    "Rob Teixeira" <RobTeixeira@@msn.com> wrote:
    >
    >I'm not exactly sure what the real question is either.
    >However, if you need a file/path utility, just look to shlwapi.dll.
    >This DLL is present on Win98 and higher (win95 if IE4.0 is present).
    >It has roughly 75 utility functions that let you deal with file and path
    >names, including URL's, which allows you to strip parts, find matches, add,
    >rename, concatinate, remove backslashes, etc.
    >
    >If you want to know if a given file has a certain extension, you can use
    >the PathMatchSpec function.
    >
    >Declare Function PathMatchSpec Lib "shlwapi.dll" _
    > alias "PathMatchSpecA" (ByVal pszFileParam As String, _
    > ByVal pszSpec As String) As Long
    >
    >. . .
    >
    >Function FileIsJPG (strFileName As String) As Boolean
    > Dim lngRetVal As Long
    >
    > lngRetVal = PathMatchSpec(strFileName, "*.jpg")
    > FileIsJPG = CBool(lngRetVal)
    >End Function
    >
    >
    >Hope that helps.
    >-Rob
    >
    >
    >
    >"Karl E. Peterson" <karl@mvps.org> wrote:
    >>Hi Mike --
    >>
    >>> I'm building a routine that builds the path to image files that are retreive
    >>> for user reviews. The files may be either file.jpg or file.tif. Any recomendations
    >>> as to which call if any to use?

    >>
    >>I'm not sure your question here quite equates to the Subject above, so

    I'll
    >assume
    >>the Subject was the more succinct way of asking?
    >>
    >>To get the extension, you can use InstrRev to find the position of the

    last
    >dot in
    >>the filename, and read what follows:
    >>
    >> nDot = InstrRev(filename$, ".")
    >> If nDot < Len(filename$) And nDot > 0 Then
    >> Ext$ = Mid$(filename$, nDot + 1)
    >> End If
    >>
    >>If you're using VB5, or would like to write more portable code, use a reverse

    >loop
    >>through the string:
    >>
    >> For i = Len(filename$) To 1 Step -1
    >> If Mid$(filename$, i, 1) = "." Then
    >> If i < Len(filename$) Then
    >> Ext$ = Mid$(filename$, i + 1)
    >> End If
    >> Exit For
    >> End If
    >> Next i
    >>
    >>That what you're after?
    >>
    >>Later... Karl
    >>--
    >>http://www.mvps.org/vb
    >>
    >>

    >



  5. #5
    L.J. Johnson Guest

    Re: Determining a file extension

    Mike,

    > Thanks!, Rob that was exactly what it took! Where can I learn more about
    > the shlwapi.dll? Thanks again.


    One of the better sites for this is VBNet (http://www.mvps.org/vbnet/).
    Randy Birch has a *lot* of examples for this.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>




  6. #6
    Karl E. Peterson Guest

    Re: Determining a file extension

    Hi Rob --

    > However, if you need a file/path utility, just look to shlwapi.dll.
    > This DLL is present on Win98 and higher (win95 if IE4.0 is present).


    Heh, I got *stung* so bad on that DLL many years ago, that I've sworn off it. I
    guess the incidence of Win95 boxes is a lot lower now, than then. But, hey, I still
    keep one here, just to avoid having users run tests for me. <g>

    Thanks... Karl


  7. #7
    Rob Teixeira Guest

    Re: Determining a file extension


    I know what you mean, but these days the only 95 systems that don't have IE4.0
    are either a brand new pre OSR2 install (right before IE4+ gets installed),
    or an alternate boot for a Linux box.

    -Rob

    "Karl E. Peterson" <karl@mvps.org> wrote:
    >Hi Rob --
    >
    >> However, if you need a file/path utility, just look to shlwapi.dll.
    >> This DLL is present on Win98 and higher (win95 if IE4.0 is present).

    >
    >Heh, I got *stung* so bad on that DLL many years ago, that I've sworn off

    it. I
    >guess the incidence of Win95 boxes is a lot lower now, than then. But,

    hey, I still
    >keep one here, just to avoid having users run tests for me. <g>
    >
    >Thanks... Karl
    >



  8. #8
    L.J. Johnson Guest

    Re: Determining a file extension

    Karl,

    > Heh, I got *stung* so bad on that DLL many years ago, that I've sworn off

    it.

    And it's *much* slower (at least according to the benchmarks on VBNet, which
    I duplicated in magnitude on my machine).

    Fortunately, *most* of the time it doesn't matter, unless you are iterating
    thru a LOT of files.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>




  9. #9
    Michael Shutt Guest

    Re: Determining a file extension

    The other suggestions will work, but you may want to check out the
    Scripting.FileSystemObject also. Add a reference to "Microsoft Scripting
    Runtime" to your project. The file system object has a handful of functions
    for extracting parts of a path or constructing a full path based on the
    parts.

    --
    Michael Shutt

    Please respond to newsgroup as I will not return direct emails.

    "Mike Kraemer" <mkraemer@91expresslanes.com> wrote in message
    news:3b61d8a5$1@news.devx.com...
    >
    > Hi Everyone!
    > I'm building a routine that builds the path to image files that are

    retreive
    > for user reviews. The files may be either file.jpg or file.tif. Any

    recomendations
    > as to which call if any to use?
    > Thanks in advance for your assistance.
    >
    > Mike




  10. #10
    Karl E. Peterson Guest

    Re: Determining a file extension

    Hi Rob --

    > I know what you mean, but these days the only 95 systems that don't have IE4.0
    > are either a brand new pre OSR2 install (right before IE4+ gets installed),
    > or an alternate boot for a Linux box.


    Or, your mom's. Or, worse, your wife's mom's. ;-)

    I agreed the incidence is low, but I guess I still hold that *if* it's doable,
    especially with ease, in "pure VB", using these hit-and-miss DLLs isn't as
    responsible in non-controlled distributions.

    Later... Karl
    --
    http://www.mvps.org/vb



  11. #11
    Karl E. Peterson Guest

    Re: Determining a file extension

    Hi L.J. --

    > > Heh, I got *stung* so bad on that DLL many years ago, that I've sworn off

    > it.
    >
    > And it's *much* slower (at least according to the benchmarks on VBNet, which
    > I duplicated in magnitude on my machine).


    Wow, that right? Further ammo for doing it the Classic way.

    > Fortunately, *most* of the time it doesn't matter, unless you are iterating
    > thru a LOT of files.


    Could be all that pointless conversion from->to->from Unicode, huh?

    Thanks... Karl
    --
    http://www.mvps.org/vb



  12. #12
    Karl E. Peterson Guest

    Re: Determining a file extension

    And that's probably the slowest of all possible methods...
    --
    http://www.mvps.org/vb

    "Michael Shutt" <mshutt_nospam@mediaone.net> wrote in message
    news:3b668a41@news.devx.com...
    > The other suggestions will work, but you may want to check out the
    > Scripting.FileSystemObject also. Add a reference to "Microsoft Scripting
    > Runtime" to your project. The file system object has a handful of functions
    > for extracting parts of a path or constructing a full path based on the
    > parts.
    >
    > --
    > Michael Shutt
    >
    > Please respond to newsgroup as I will not return direct emails.
    >
    > "Mike Kraemer" <mkraemer@91expresslanes.com> wrote in message
    > news:3b61d8a5$1@news.devx.com...
    > >
    > > Hi Everyone!
    > > I'm building a routine that builds the path to image files that are

    > retreive
    > > for user reviews. The files may be either file.jpg or file.tif. Any

    > recomendations
    > > as to which call if any to use?
    > > Thanks in advance for your assistance.
    > >
    > > Mike

    >
    >



  13. #13
    L.J. Johnson Guest

    Re: Determining a file extension

    Karl,

    > And that's probably the slowest of all possible methods...


    Yes, but let's be fair <g>. At least in the programs I've worked on, *most*
    of the times I work with somewhere between a couple of files, and a couple
    of dozen. You'd never see the speed difference here. It *does*, of course,
    require an additional DLL, but some of the routine are darn handy.

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>





  14. #14
    Karl E. Peterson Guest

    Re: Determining a file extension

    Hi L.J. --

    > > And that's probably the slowest of all possible methods...

    >
    > Yes, but let's be fair <g>. At least in the programs I've worked on, *most*
    > of the times I work with somewhere between a couple of files, and a couple
    > of dozen. You'd never see the speed difference here. It *does*, of course,
    > require an additional DLL, but some of the routine are darn handy.


    Depends what you're up to. In most simple cases, that's very true. The problems
    occur when folks write these as "generic" routines, then try to scale them to the
    trouble cases. The bottom line is, there can easily be *two orders* of magnitude
    difference, based on the choices made:

    http://www.mvps.org/vbnet/code/filea...apicompare.htm

    Might as well just invest the time to do it right the first time, then reap the
    benefits from there on out.

    Later... Karl
    --
    http://www.mvps.org/vb



  15. #15
    L.J. Johnson Guest

    Re: Determining a file extension

    Karl,

    > Depends what you're up to. In most simple cases, that's very true. The

    problems
    > occur when folks write these as "generic" routines, then try to scale them

    to the
    > trouble cases. The bottom line is, there can easily be *two orders* of

    magnitude
    > difference, based on the choices made:
    >
    > Might as well just invest the time to do it right the first time, then

    reap the
    > benefits from there on out.


    I have mixed feelings, but I've used them before <shame>.

    Might be interesting to wrapper *all* the functions in VB/API code as an
    stand-alone dll (which would be faster, to boot). Think it's worth wasting
    an article on?

    --
    L.J. Johnson, Slightly Tilted Software
    Microsoft MVP (Visual Basic)
    LJJohnson@SlightlyTiltedSoftware.com or LJJohnson@mvps.org
    <http://www.SlightlyTiltedSoftware.com>
    Ask The NT Pro at <http://www.devx.com/gethelp>




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center