943,789 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 34970
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2007
0

Extract the file name from the full file path.

Expand Post »
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 is full path, like this,

C++ Syntax (Toggle Plain Text)
  1. G:\Work On\CPP\001_002_003.txt

What I want to do is, find the name of the file(without the extension), 001_002_003 according to my example. Then separate as 001 002 and 003, then write them to a file in three columns.

I'm stuck with how to get that three part, 001 002 and 003, from the full file path.

Thanks.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

DOS used to have a 'splitpath' function which returned the various components of a full path.

Or you could do
- find the last \ to mark the start of the filename
- find the last . to mark the end of the filename.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

Click to Expand / Collapse  Quote originally posted by Salem ...
Or you could do
- find the last \ to mark the start of the filename
- find the last . to mark the end of the filename.
Can you tell me about this more. Any worked example really appreciate.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

Actually, you should probably Read This!, then post back.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

Great.......
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

> Can you tell me about this more. Any worked example really appreciate.
What did your last slave die of?

Are you totally incapable of digging around in the manual pages to see what the CString API is capable of, or do you have to be told every single detail in advance before you try to use it?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

Ok, at the end I have done it myself using MSDN. Used CFindFile class and FindNextFile() to do that. Like this.

C++ Syntax (Toggle Plain Text)
  1. CFileFind fileName;
  2.  
  3. static const TCHAR UseFile[] = _T("F:\\Resources\\Files\\ReadFile.txt");
  4.  
  5. BOOL ress = fileName.FindFile(UseFile);
  6. fileName.FindNextFile();
  7. if(ress)
  8. {
  9. AfxMessageBox((LPCSTR)fileName.GetFileTitle(), MB_OK);
  10. }

I want to use a CString here, because file path is on CString format, which I have already found. Can you give me a clue how to use it here in this code.

That mean I have to cast CString to a char buffer, is that right? Is it possible?
Last edited by eranga262154; Nov 8th, 2007 at 7:46 am. Reason: Adding more
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

OMG why in the world are you doing that just to extract the name part of the string! If the CString already contains the full path + filename just use its Find() method to locate the last '\' and you have the name starting with the next character.

>>That mean I have to cast CString to a char buffer, is that right? Is it possible?
Depends -- is your program compiled for UNICODE or not. If it is, then CString can not be simply typecase, it must be converted from wchar_t* to char* with one of the conversion functions.
Last edited by Ancient Dragon; Nov 8th, 2007 at 8:38 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 8th, 2007
0

Re: Extract the file name from the full file path.

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.
C++ Syntax (Toggle Plain Text)
  1. CString ss = "F:\\Files\\ReadFile.txt";
  2. CString aa;
  3.  
  4. if(ss.ReverseFind('\\')== 8)
  5. {
  6. AfxMessageBox(ss.Right(12), MB_OK);
  7. aa = ss.Right(12);
  8. }
  9.  
  10. if(aa.ReverseFind('.')==8)
  11. {
  12. AfxMessageBox(aa.Left(8), MB_OK);
  13. }

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.
Last edited by eranga262154; Nov 8th, 2007 at 11:36 pm. Reason: Missing code tags
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Nov 9th, 2007
0

Re: Extract the file name from the full file path.

> if(ss.ReverseFind('\\')== 8) What does this return if there is no \\ ?

In other words, do something like
c++ Syntax (Toggle Plain Text)
  1. int pathStart = ss.ReverseFind('\\');
  2. if ( pathStart >= 0 ) {
  3. // using the position and length of the string, you can now do say
  4. aa = ss.Right(12);
  5. // but replace the 12 with some kind of calculation based on the dynamic
  6. // results you've calculated so far
  7. }

Also, don't assume that the extension is
a) always present
b) always 3 characters.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Message:
Previous Thread in C++ Forum Timeline: Help me convert this stuck to class program
Next Thread in C++ Forum Timeline: auto_ptr assign to shared_ptr???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC