| | |
Extract the file name from the full file path.
![]() |
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,
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.
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)
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.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Actually, you should probably Read This!, then post back.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Ok, at the end I have done it myself using MSDN. Used CFindFile class and FindNextFile() to do that. Like this.
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?
C++ Syntax (Toggle Plain Text)
CFileFind fileName; static const TCHAR UseFile[] = _T("F:\\Resources\\Files\\ReadFile.txt"); BOOL ress = fileName.FindFile(UseFile); fileName.FindNextFile(); if(ress) { AfxMessageBox((LPCSTR)fileName.GetFileTitle(), MB_OK); }
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
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
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.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
C++ Syntax (Toggle Plain Text)
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); }
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
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
>
In other words, do something like
Also, don't assume that the extension is
a) always present
b) always 3 characters.
if(ss.ReverseFind('\\')== 8) What does this return if there is no \\ ?In other words, do something like
c++ Syntax (Toggle Plain Text)
int pathStart = ss.ReverseFind('\\'); if ( pathStart >= 0 ) { // using the position and length of the string, you can now do say aa = ss.Right(12); // but replace the 12 with some kind of calculation based on the dynamic // results you've calculated so far }
Also, don't assume that the extension is
a) always present
b) always 3 characters.
![]() |
Similar Threads
- Unable to open Web Project '...'. The file path '...' does not correspond to the URL (ASP.NET)
- Unable to open Web Project '...'. The file path '...' does not correspond to the URL (Community Introductions)
- please help me remove winfix. here is my log file from Hijack this (Viruses, Spyware and other Nasties)
- getting rid of Admilliserve log file (Viruses, Spyware and other Nasties)
- About;Blank Please Help, Hijack Log File (Viruses, Spyware and other Nasties)
- How to write a header file (C++)
- ASP locating file at client side (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: point inside triangle any situations where my code will fail?
- Next Thread: C++ postfix to infix help!
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output pointer problem program programming project python random read recursion reference rpg shutdown() std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






