Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting file extension from path

Getting file extension from path

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ __DanC__

    This is hopefully a basic question but it's been a VERY long time since I used C++ and I just need to know how I can get a file extension from a path and compare to constant extension. I have an input: LPCWSTR pwszPath and a constant: WCHAR MY_EXT[4] = L".me"; I need to know if pwszPath ends with MY_EXT but I can't think how to do it.. Any help would be great!

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #2

    __DanC__ wrote:

    I need to know if pwszPath ends with MY_EXT but I can't think how to do it..

    Use PathFindExtension. Matching extension will somewhat look like this...

    // Assumption: there is no '.' in extension
    bool MatchExtension( LPCTSTR lpctszMyPath, LPCTSTR lpctszChkExt )
    {
    ASSERT( lpctszMyPath && lpctszChkExt );
    LPCTSTR lpctszExt = PathFindExtension( lpctszMyPath );
    // Or if you don't wish to link to shlwapi for this one function use
    // LPCTSTR lpctszExt = _tcsrchr( lpctszMyPath, _T( '.' ));
    return ( lpctszExt && _tcscmpi( lpctszExt + 1, lpctszChkExt ) == 0 );
    }

    Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

    modified on Wednesday, July 2, 2008 5:06 AM

    _ 1 Reply Last reply
    0
    • _ __DanC__

      This is hopefully a basic question but it's been a VERY long time since I used C++ and I just need to know how I can get a file extension from a path and compare to constant extension. I have an input: LPCWSTR pwszPath and a constant: WCHAR MY_EXT[4] = L".me"; I need to know if pwszPath ends with MY_EXT but I can't think how to do it.. Any help would be great!

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #3

      What about PathFindExtension [^]? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      N 1 Reply Last reply
      0
      • N Nibu babu thomas

        __DanC__ wrote:

        I need to know if pwszPath ends with MY_EXT but I can't think how to do it..

        Use PathFindExtension. Matching extension will somewhat look like this...

        // Assumption: there is no '.' in extension
        bool MatchExtension( LPCTSTR lpctszMyPath, LPCTSTR lpctszChkExt )
        {
        ASSERT( lpctszMyPath && lpctszChkExt );
        LPCTSTR lpctszExt = PathFindExtension( lpctszMyPath );
        // Or if you don't wish to link to shlwapi for this one function use
        // LPCTSTR lpctszExt = _tcsrchr( lpctszMyPath, _T( '.' ));
        return ( lpctszExt && _tcscmpi( lpctszExt + 1, lpctszChkExt ) == 0 );
        }

        Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        modified on Wednesday, July 2, 2008 5:06 AM

        _ Offline
        _ Offline
        __DanC__
        wrote on last edited by
        #4

        That was exactly what I was looking for! Thanks!

        N 1 Reply Last reply
        0
        • _ __DanC__

          That was exactly what I was looking for! Thanks!

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #5

          __DanC__ wrote:

          That was exactly what I was looking for! Thanks!

          I've modified my reply a bit. :)

          Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          1 Reply Last reply
          0
          • C CPallini

            What about PathFindExtension [^]? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #6

            too late buddy :)

            nave [OpenedFileFinder]

            C 1 Reply Last reply
            0
            • N Naveen

              too late buddy :)

              nave [OpenedFileFinder]

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              Indeed: bits flow very slowly from Italy this morning :zzz:. :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              N N 2 Replies Last reply
              0
              • C CPallini

                Indeed: bits flow very slowly from Italy this morning :zzz:. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #8

                :laugh: :laugh:

                nave [OpenedFileFinder]

                1 Reply Last reply
                0
                • C CPallini

                  Indeed: bits flow very slowly from Italy this morning :zzz:. :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #9

                  Quote: bits flow very slowly from Italy this morning Bits? or Thoughts? :P

                  Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                  C 1 Reply Last reply
                  0
                  • N Nelek

                    Quote: bits flow very slowly from Italy this morning Bits? or Thoughts? :P

                    Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #10

                    I don't know (maybe bits of thougths? :-D). BTW apparently the same happens from Germany...;P (this thread is a bit dated)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    N 1 Reply Last reply
                    0
                    • C CPallini

                      I don't know (maybe bits of thougths? :-D). BTW apparently the same happens from Germany...;P (this thread is a bit dated)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      N Offline
                      N Offline
                      Nelek
                      wrote on last edited by
                      #11

                      Yeah, I am having quite things to do and less time to do them, I am a bit away from keyboard

                      Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups