dcsimg


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

  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
    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



  4. #4
    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
    >
    >



  5. #5
    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
    >
    >



  6. #6
    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
    >>
    >>

    >



  7. #7
    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
    >>
    >>

    >



  8. #8
    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>




  9. #9
    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>




  10. #10
    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


  11. #11
    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


  12. #12
    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
    >



  13. #13
    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
    >



  14. #14
    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>




  15. #15
    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>




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