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. How to get file name from a full path

How to get file name from a full path

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsontutorialquestioncareer
25 Posts 8 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.
  • Z zengkun100

    Thank you nave! I think I have got the answer:_splitpath :)

    A Chinese VC++ programmer

    A Offline
    A Offline
    Abhay Menon
    wrote on last edited by
    #6

    You can use the CFileFind CFileFind cf; cf.FindFile("C:\\Program Files\\codeproject.txt"); cf.FindNext(); CString strFileName = cf.GetFileName(); Rgds Abhay..

    1 Reply Last reply
    0
    • Z zengkun100

      Suppose the full path is : C:\Program Files\codeproject.txt I want to extract the file name codeproject.txt. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)

      A Chinese VC++ programmer

      D Offline
      D Offline
      Don Box
      wrote on last edited by
      #7

      Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.

      Come online at:- jubinc@skype

      N Z 2 Replies Last reply
      0
      • D Don Box

        Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.

        Come online at:- jubinc@skype

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

        Don Box wrote:

        still I'll strongly recommend u to try writing such function.

        Less u write, less will be the bugs...

        nave [OpenedFileFinder]

        D R 2 Replies Last reply
        0
        • N Naveen

          Don Box wrote:

          still I'll strongly recommend u to try writing such function.

          Less u write, less will be the bugs...

          nave [OpenedFileFinder]

          D Offline
          D Offline
          Don Box
          wrote on last edited by
          #9

          If basics are not clear and don't have confidence, then better don't write (such simple function) and drop the programming profession.

          Come online at:- jubinc@skype

          N 1 Reply Last reply
          0
          • D Don Box

            Though some API's have been mentioned above by our codeproject friends, u can also write such functions. If u know 'C' language very well and ofcourse, its basics (people having lot of experience also doesn't know the basics), u can easily write such functions. Though u got the API, still I'll strongly recommend u to try writing such function.

            Come online at:- jubinc@skype

            Z Offline
            Z Offline
            zengkun100
            wrote on last edited by
            #10

            I do have written a function to do make the job done, but I still want to know is there some exist functions can do this job for me. :)

            A Chinese VC++ programmer

            D 1 Reply Last reply
            0
            • D Don Box

              If basics are not clear and don't have confidence, then better don't write (such simple function) and drop the programming profession.

              Come online at:- jubinc@skype

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

              cool man..I just said one truth regrding the programming field. How ever I still remeber one bug while trying to extract the file name from path. We were extracting the file name by reverse finding the "\". But later a bug was reported and reason was that, in one secnario in the path input to that function, instead of "\", seperator was "/" :(

              nave [OpenedFileFinder]

              D 1 Reply Last reply
              0
              • N Naveen

                cool man..I just said one truth regrding the programming field. How ever I still remeber one bug while trying to extract the file name from path. We were extracting the file name by reverse finding the "\". But later a bug was reported and reason was that, in one secnario in the path input to that function, instead of "\", seperator was "/" :(

                nave [OpenedFileFinder]

                D Offline
                D Offline
                Don Box
                wrote on last edited by
                #12

                That's right Naveen. BTW, how many yrs of experience do u have in VC++? Do u have any Skype/Yahoo id?

                Come online at:- jubinc@skype

                N 1 Reply Last reply
                0
                • Z zengkun100

                  I do have written a function to do make the job done, but I still want to know is there some exist functions can do this job for me. :)

                  A Chinese VC++ programmer

                  D Offline
                  D Offline
                  Don Box
                  wrote on last edited by
                  #13

                  _splitpath() is better option.

                  Come online at:- jubinc@skype

                  1 Reply Last reply
                  0
                  • N Naveen

                    Don Box wrote:

                    still I'll strongly recommend u to try writing such function.

                    Less u write, less will be the bugs...

                    nave [OpenedFileFinder]

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #14

                    Naveen wrote:

                    Less u write, less will be the bugs...

                    Write no code and you'll have absolutely no bugs. :-\

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    N 1 Reply Last reply
                    0
                    • Z zengkun100

                      Suppose the full path is : C:\Program Files\codeproject.txt I want to extract the file name codeproject.txt. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)

                      A Chinese VC++ programmer

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #15

                      You may use PathFindFileName() like this:

                      TCHAR \*szPath = \_T("C:\\\\Program Files\\\\codeproject.txt");
                      AfxMessageBox(PathFindFileName(szPath));
                      

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      Z 1 Reply Last reply
                      0
                      • Z zengkun100

                        Suppose the full path is : C:\Program Files\codeproject.txt I want to extract the file name codeproject.txt. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)

                        A Chinese VC++ programmer

                        F Offline
                        F Offline
                        fd0129002
                        wrote on last edited by
                        #16

                        #include <Shlwapi.h>; #pragma comment(lib, "shlwapi.lib") LPTSTR lpszFileName = PathFindFileName(szPath);

                        ============ Einstein Seeing is believing.

                        1 Reply Last reply
                        0
                        • Z zengkun100

                          Thank you nave! I think I have got the answer:_splitpath :)

                          A Chinese VC++ programmer

                          R Offline
                          R Offline
                          Rajesh R Subramanian
                          wrote on last edited by
                          #17

                          zengkun100 wrote:

                          I think I have got the answer:_splitpath

                          MSDN Says: _splitpath is deprecated because more secure versions are available, see _splitpath_s, _wsplitpath_s.

                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                          1 Reply Last reply
                          0
                          • R Rajesh R Subramanian

                            Naveen wrote:

                            Less u write, less will be the bugs...

                            Write no code and you'll have absolutely no bugs. :-\

                            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

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

                            Yes. but the damn requirments doesnt allow me to be like that :(

                            nave [OpenedFileFinder]

                            1 Reply Last reply
                            0
                            • D Don Box

                              That's right Naveen. BTW, how many yrs of experience do u have in VC++? Do u have any Skype/Yahoo id?

                              Come online at:- jubinc@skype

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

                              Don Box wrote:

                              Do u have any Skype/Yahoo id?

                              Yahoo only... Have send my chat id to your mail box. check

                              nave [OpenedFileFinder]

                              D 1 Reply Last reply
                              0
                              • N Naveen

                                Don Box wrote:

                                Do u have any Skype/Yahoo id?

                                Yahoo only... Have send my chat id to your mail box. check

                                nave [OpenedFileFinder]

                                D Offline
                                D Offline
                                Don Box
                                wrote on last edited by
                                #20

                                Naveen wrote:

                                Yahoo only... Have send my chat id to your mail box. check

                                To my which e-mail id? If it is skype, then there's no mail box for this. Its my skype id (just for chatting).

                                Come online at:- jubinc@skype

                                N 1 Reply Last reply
                                0
                                • D Don Box

                                  Naveen wrote:

                                  Yahoo only... Have send my chat id to your mail box. check

                                  To my which e-mail id? If it is skype, then there's no mail box for this. Its my skype id (just for chatting).

                                  Come online at:- jubinc@skype

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

                                  No I clicked "email" option in the message board. My chat id is nave432@yahoo.com

                                  nave [OpenedFileFinder]

                                  1 Reply Last reply
                                  0
                                  • Z zengkun100

                                    Suppose the full path is : C:\Program Files\codeproject.txt I want to extract the file name codeproject.txt. Of course I can use CString class to find the last '\', and then Mid to get the filename, but is there some API can do this daily job? Thank you :)

                                    A Chinese VC++ programmer

                                    N Offline
                                    N Offline
                                    Nitheesh George
                                    wrote on last edited by
                                    #22

                                    Hi, char szFilePath[MAX_PATH] = {"C:\\Program Files\\codeproject.txt"}; char szFileNameOnly = strrchr(szFilePath,'\\'); if(szFileNameOnly) szFileNameOnly++; now szFileName points to "codeproject.txt" i think this works fine for u.

                                    R 1 Reply Last reply
                                    0
                                    • N Nitheesh George

                                      Hi, char szFilePath[MAX_PATH] = {"C:\\Program Files\\codeproject.txt"}; char szFileNameOnly = strrchr(szFilePath,'\\'); if(szFileNameOnly) szFileNameOnly++; now szFileName points to "codeproject.txt" i think this works fine for u.

                                      R Offline
                                      R Offline
                                      Rajesh R Subramanian
                                      wrote on last edited by
                                      #23

                                      Nitheesh George wrote:

                                      char szFileNameOnly = strrchr(szFilePath,'\\');

                                      Good, but a small correction. strrchr returns a character pointer. :)

                                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                      1 Reply Last reply
                                      0
                                      • R Rajesh R Subramanian

                                        You may use PathFindFileName() like this:

                                        TCHAR \*szPath = \_T("C:\\\\Program Files\\\\codeproject.txt");
                                        AfxMessageBox(PathFindFileName(szPath));
                                        

                                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                        Z Offline
                                        Z Offline
                                        zengkun100
                                        wrote on last edited by
                                        #24

                                        Rajesh R Subramanian wrote:

                                        PathFindFileName()

                                        Thank you Rajesh R Subramanian I think this is the simplest way I have ever seen :-D

                                        A Chinese VC++ programmer

                                        R 1 Reply Last reply
                                        0
                                        • Z zengkun100

                                          Rajesh R Subramanian wrote:

                                          PathFindFileName()

                                          Thank you Rajesh R Subramanian I think this is the simplest way I have ever seen :-D

                                          A Chinese VC++ programmer

                                          R Offline
                                          R Offline
                                          Rajesh R Subramanian
                                          wrote on last edited by
                                          #25

                                          zengkun100 wrote:

                                          Thank you Rajesh

                                          You're welcome. :)

                                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                          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