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. Extract the file name from the full file path

Extract the file name from the full file path

Scheduled Pinned Locked Moved C / C++ / MFC
help
22 Posts 10 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.
  • C CodingLover

    Hi all, Using my application I've found a file path anywhere on my machine and store the full path in a CString variable. That full path, like this, "F:\\Files\\Read_File.txt" What I want to do is, find the name of the file(without the extension), and then separate two words Read and File. I have tried one thing. Use ReverseFind() method of CString class to find the last '\' sign. Then I used the Right() to get the required file name. I can use Right() easily because file name length is always fixed. CString ss = "F:\\Files\\ReadFile.txt"; CString aa; if(ss.ReverseFind('\\')== 8) { AfxMessageBox(ss.Right(12), MB_OK); aa = ss.Right(12); } if(aa.ReverseFind('.')==8) { AfxMessageBox(aa.Left(8), MB_OK); } In this way I can separate those two words as well. Actually this code gives the file name, ReadFile. But there is a real issue, on the first 'if' condition. First part of the path, that is "F:\\Files" is not fixed. So in the first 'if' condition number 8 can be change depend on the file selection on my PC. How can avoid that. Thanks

    I appreciate your help all the time... Eranga :)

    L Offline
    L Offline
    Llasus
    wrote on last edited by
    #2

    ReverseFind gives you the location in the string of the certain character you are searching for. Using that it can already be flexible regardless of the length of the filename. To get the application name you can use: aa = ss.Right(ss.GetLength() - (ss.ReverseFind('\\') + 1)); //add 1 to exclude the '\' with some adjustments, almost the same procedure can be done to get the file name (without the extension) aa.Left(aa.ReverseFind('.')); For flexibility you can try to pass the result of ReverseFind to an int first to check if it returns -1 (character not found).

    C 1 Reply Last reply
    0
    • L Llasus

      ReverseFind gives you the location in the string of the certain character you are searching for. Using that it can already be flexible regardless of the length of the filename. To get the application name you can use: aa = ss.Right(ss.GetLength() - (ss.ReverseFind('\\') + 1)); //add 1 to exclude the '\' with some adjustments, almost the same procedure can be done to get the file name (without the extension) aa.Left(aa.ReverseFind('.')); For flexibility you can try to pass the result of ReverseFind to an int first to check if it returns -1 (character not found).

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

      Ok, I've do it in this way. CString ss = "F:\\SRF Resources\\SRF\\G00166_003_01.srf"; CString aa, bb; aa = ss.Right(ss.GetLength() - (ss.ReverseFind('\\') + 1)); //add 1 to exclude the '\' AfxMessageBox(aa, MB_OK); if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); } What is your comments.

      I appreciate your help all the time... Eranga :)

      L D 2 Replies Last reply
      0
      • C CodingLover

        Ok, I've do it in this way. CString ss = "F:\\SRF Resources\\SRF\\G00166_003_01.srf"; CString aa, bb; aa = ss.Right(ss.GetLength() - (ss.ReverseFind('\\') + 1)); //add 1 to exclude the '\' AfxMessageBox(aa, MB_OK); if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); } What is your comments.

        I appreciate your help all the time... Eranga :)

        L Offline
        L Offline
        Llasus
        wrote on last edited by
        #4

        Eranga Thennakoon wrote:

        if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); }

        Even if you have a fixed filename length wouldn't it be better to just use: if(aa.ReverseFind('.') != -1) { bb = aa.Left(aa.ReverseFind('.')); AfxMessageBox(bb); } It is much more flexible if the filename was changed to another one. Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

        C 2 Replies Last reply
        0
        • L Llasus

          Eranga Thennakoon wrote:

          if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); }

          Even if you have a fixed filename length wouldn't it be better to just use: if(aa.ReverseFind('.') != -1) { bb = aa.Left(aa.ReverseFind('.')); AfxMessageBox(bb); } It is much more flexible if the filename was changed to another one. Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #5

          Llasus wrote:

          Even if you have a fixed filename length wouldn't it be better to just use:

          Yep, it's better. At the same time I've try another way to do this, but felt so mess. Use Find() to get the last '\' and then read backward to find the name. Try to follow the same way to find '.' and avoid extension. Failed. :->

          Llasus wrote:

          Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

          Thanks pal for the infor. I just used it, because I feel it is completed my code in view. Actually it is useless. I have to type more and more when I used message box. Actually on my testings I used more message boxes to verify my output. :-D

          I appreciate your help all the time... Eranga :)

          L D 2 Replies Last reply
          0
          • C CodingLover

            Llasus wrote:

            Even if you have a fixed filename length wouldn't it be better to just use:

            Yep, it's better. At the same time I've try another way to do this, but felt so mess. Use Find() to get the last '\' and then read backward to find the name. Try to follow the same way to find '.' and avoid extension. Failed. :->

            Llasus wrote:

            Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

            Thanks pal for the infor. I just used it, because I feel it is completed my code in view. Actually it is useless. I have to type more and more when I used message box. Actually on my testings I used more message boxes to verify my output. :-D

            I appreciate your help all the time... Eranga :)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            Hi, for viewing intermediate results I seldom use MessageBox. I prefer either writing to the console (which is the output pane in Visual Studio while debugging) as printf() does, and/or writing to a log file. In both cases the advantages are: you don't have to click OK buttons all the time, and you get to see a lot of intermediate results on consecutive lines, seems easier to figure out where things start to go wrong. Anyway, I tend to create a "void log(char*)" function that does the above (and appends a newline if appropriate). :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


            C N 2 Replies Last reply
            0
            • C CodingLover

              Hi all, Using my application I've found a file path anywhere on my machine and store the full path in a CString variable. That full path, like this, "F:\\Files\\Read_File.txt" What I want to do is, find the name of the file(without the extension), and then separate two words Read and File. I have tried one thing. Use ReverseFind() method of CString class to find the last '\' sign. Then I used the Right() to get the required file name. I can use Right() easily because file name length is always fixed. CString ss = "F:\\Files\\ReadFile.txt"; CString aa; if(ss.ReverseFind('\\')== 8) { AfxMessageBox(ss.Right(12), MB_OK); aa = ss.Right(12); } if(aa.ReverseFind('.')==8) { AfxMessageBox(aa.Left(8), MB_OK); } In this way I can separate those two words as well. Actually this code gives the file name, ReadFile. But there is a real issue, on the first 'if' condition. First part of the path, that is "F:\\Files" is not fixed. So in the first 'if' condition number 8 can be change depend on the file selection on my PC. How can avoid that. Thanks

              I appreciate your help all the time... Eranga :)

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

              Check the PathStripPath() function

              nave [OpenedFileFinder]

              C 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, for viewing intermediate results I seldom use MessageBox. I prefer either writing to the console (which is the output pane in Visual Studio while debugging) as printf() does, and/or writing to a log file. In both cases the advantages are: you don't have to click OK buttons all the time, and you get to see a lot of intermediate results on consecutive lines, seems easier to figure out where things start to go wrong. Anyway, I tend to create a "void log(char*)" function that does the above (and appends a newline if appropriate). :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                C Offline
                C Offline
                CodingLover
                wrote on last edited by
                #8

                Thanks pal. Actually lots things I have done on MFC. So that's why I used that way to check weather I'm going wrong or not. :)

                I appreciate your help all the time... Eranga :)

                1 Reply Last reply
                0
                • N Naveen

                  Check the PathStripPath() function

                  nave [OpenedFileFinder]

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #9

                  Thanks pal. But I feel that ReversFind() more easy, because I can use Mid() to extract any sub-string from the file name. Thanks again. :-D

                  I appreciate your help all the time... Eranga :)

                  I 1 Reply Last reply
                  0
                  • L Llasus

                    Eranga Thennakoon wrote:

                    if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); }

                    Even if you have a fixed filename length wouldn't it be better to just use: if(aa.ReverseFind('.') != -1) { bb = aa.Left(aa.ReverseFind('.')); AfxMessageBox(bb); } It is much more flexible if the filename was changed to another one. Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

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

                    I have a mess here. I no it is complicated that this way. CString::Find() Thing is that, can I used index to get a sub-string. Basically it searches the sub-string which we define. Then gives ASSERT the application. What is your comments on my thought. :|

                    I appreciate your help all the time... Eranga :)

                    L H 2 Replies Last reply
                    0
                    • C CodingLover

                      I have a mess here. I no it is complicated that this way. CString::Find() Thing is that, can I used index to get a sub-string. Basically it searches the sub-string which we define. Then gives ASSERT the application. What is your comments on my thought. :|

                      I appreciate your help all the time... Eranga :)

                      L Offline
                      L Offline
                      Llasus
                      wrote on last edited by
                      #11

                      Can you give the snippet of your code which causes the assert error?

                      C 1 Reply Last reply
                      0
                      • C CodingLover

                        I have a mess here. I no it is complicated that this way. CString::Find() Thing is that, can I used index to get a sub-string. Basically it searches the sub-string which we define. Then gives ASSERT the application. What is your comments on my thought. :|

                        I appreciate your help all the time... Eranga :)

                        H Offline
                        H Offline
                        Hamid Taebi
                        wrote on last edited by
                        #12

                        Eranga Thennakoon wrote:

                        Then gives ASSERT the application

                        what was your string?

                        C 1 Reply Last reply
                        0
                        • L Llasus

                          Can you give the snippet of your code which causes the assert error?

                          C Offline
                          C Offline
                          CodingLover
                          wrote on last edited by
                          #13

                          Actually I don't have a code. I asked this based on MSDN example. I try it coding and let you know. :)

                          I appreciate your help all the time... Eranga :)

                          1 Reply Last reply
                          0
                          • H Hamid Taebi

                            Eranga Thennakoon wrote:

                            Then gives ASSERT the application

                            what was your string?

                            C Offline
                            C Offline
                            CodingLover
                            wrote on last edited by
                            #14

                            You mean the CString which I used. It's like this, a path of a file. F:\Resources\Files\G00166_003_01.txt

                            I appreciate your help all the time... Eranga :)

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Hi, for viewing intermediate results I seldom use MessageBox. I prefer either writing to the console (which is the output pane in Visual Studio while debugging) as printf() does, and/or writing to a log file. In both cases the advantages are: you don't have to click OK buttons all the time, and you get to see a lot of intermediate results on consecutive lines, seems easier to figure out where things start to go wrong. Anyway, I tend to create a "void log(char*)" function that does the above (and appends a newline if appropriate). :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


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

                              Or use TRACE

                              Greetings. -------- 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 ;)

                              1 Reply Last reply
                              0
                              • C CodingLover

                                Ok, I've do it in this way. CString ss = "F:\\SRF Resources\\SRF\\G00166_003_01.srf"; CString aa, bb; aa = ss.Right(ss.GetLength() - (ss.ReverseFind('\\') + 1)); //add 1 to exclude the '\' AfxMessageBox(aa, MB_OK); if(aa.ReverseFind('.') == 13) { bb = aa.Left(13); AfxMessageBox(bb, MB_OK); } What is your comments.

                                I appreciate your help all the time... Eranga :)

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #16

                                Eranga Thennakoon wrote:

                                What is your comments.

                                Your code is inefficient, and makes many assumptions.


                                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                1 Reply Last reply
                                0
                                • C CodingLover

                                  Llasus wrote:

                                  Even if you have a fixed filename length wouldn't it be better to just use:

                                  Yep, it's better. At the same time I've try another way to do this, but felt so mess. Use Find() to get the last '\' and then read backward to find the name. Try to follow the same way to find '.' and avoid extension. Failed. :->

                                  Llasus wrote:

                                  Also, you can just use AfxMessageBox(bb); instead of AfxMessageBox(bb, MB_OK); since the default for AfxMessageBox is the one with OK button anyway.

                                  Thanks pal for the infor. I just used it, because I feel it is completed my code in view. Actually it is useless. I have to type more and more when I used message box. Actually on my testings I used more message boxes to verify my output. :-D

                                  I appreciate your help all the time... Eranga :)

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #17

                                  Eranga Thennakoon wrote:

                                  Use Find() to get the last '\' and then read backward to find the name. Try to follow the same way to find '.' and avoid extension. Failed.

                                  Why mess with all of this, where there are existing functions that do it for you? Will your code work with other character sets?


                                  "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                  1 Reply Last reply
                                  0
                                  • C CodingLover

                                    Thanks pal. But I feel that ReversFind() more easy, because I can use Mid() to extract any sub-string from the file name. Thanks again. :-D

                                    I appreciate your help all the time... Eranga :)

                                    I Offline
                                    I Offline
                                    Iain Clarke Warrior Programmer
                                    wrote on last edited by
                                    #18

                                    I'd strongly recommend using the ShellAPI command that David pointed you to. What about c:file.txt? or all sorts of strange notations for filenames you will hardly ever use, but the shell team have handled for you? Iain.

                                    D 1 Reply Last reply
                                    0
                                    • I Iain Clarke Warrior Programmer

                                      I'd strongly recommend using the ShellAPI command that David pointed you to. What about c:file.txt? or all sorts of strange notations for filenames you will hardly ever use, but the shell team have handled for you? Iain.

                                      D Offline
                                      D Offline
                                      David Crow
                                      wrote on last edited by
                                      #19

                                      Or what about those that are named file.jpg.exe on machines with "Hide extensions for known file types" turned off? The possibilities are not endless, but there are several of them to consider when rolling your own implementation.


                                      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                      B 1 Reply Last reply
                                      0
                                      • C CodingLover

                                        Hi all, Using my application I've found a file path anywhere on my machine and store the full path in a CString variable. That full path, like this, "F:\\Files\\Read_File.txt" What I want to do is, find the name of the file(without the extension), and then separate two words Read and File. I have tried one thing. Use ReverseFind() method of CString class to find the last '\' sign. Then I used the Right() to get the required file name. I can use Right() easily because file name length is always fixed. CString ss = "F:\\Files\\ReadFile.txt"; CString aa; if(ss.ReverseFind('\\')== 8) { AfxMessageBox(ss.Right(12), MB_OK); aa = ss.Right(12); } if(aa.ReverseFind('.')==8) { AfxMessageBox(aa.Left(8), MB_OK); } In this way I can separate those two words as well. Actually this code gives the file name, ReadFile. But there is a real issue, on the first 'if' condition. First part of the path, that is "F:\\Files" is not fixed. So in the first 'if' condition number 8 can be change depend on the file selection on my PC. How can avoid that. Thanks

                                        I appreciate your help all the time... Eranga :)

                                        A Offline
                                        A Offline
                                        Andy Moore
                                        wrote on last edited by
                                        #20

                                        How about PathFindFileName in shlwapi.dll?

                                        Pax Domini sit semper vobiscum

                                        C 1 Reply Last reply
                                        0
                                        • A Andy Moore

                                          How about PathFindFileName in shlwapi.dll?

                                          Pax Domini sit semper vobiscum

                                          C Offline
                                          C Offline
                                          CodingLover
                                          wrote on last edited by
                                          #21

                                          Thanks pal. But I think dealing with ReversFind() is so easy. :-O

                                          I appreciate your help all the time... Eranga :)

                                          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