File Path error [SOLVED]
-
i am creating a function IsFolderExists(LPTSTR str) str = "C:\New Folder"; //It Fails to get the Window Folder but when i str = "C:\\New Folder"; //It gets the Windows Folder am i doing wrong, or \\ is required. If required then how to build a path Example : C:\New Folder into C:\\New Folder. Help.....:confused::confused:
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}void IsFolderExists(LPTSTR str)
{WIN32\_FIND\_DATA wfd; HANDLE hFile; hFile = FindFirstFile(str,&wfd); if ( hFile != INVALID\_HANDLE\_VALUE ) { if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0) MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK); else MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK); } else { MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK); }
}
When i drag a folder [ NewFolder] on myapp.exe it works but when i drag a folder having space [ New Folder ] it doesn't work. please help...
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, January 22, 2011 12:42 AM
-
i am creating a function IsFolderExists(LPTSTR str) str = "C:\New Folder"; //It Fails to get the Window Folder but when i str = "C:\\New Folder"; //It gets the Windows Folder am i doing wrong, or \\ is required. If required then how to build a path Example : C:\New Folder into C:\\New Folder. Help.....:confused::confused:
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}void IsFolderExists(LPTSTR str)
{WIN32\_FIND\_DATA wfd; HANDLE hFile; hFile = FindFirstFile(str,&wfd); if ( hFile != INVALID\_HANDLE\_VALUE ) { if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0) MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK); else MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK); } else { MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK); }
}
When i drag a folder [ NewFolder] on myapp.exe it works but when i drag a folder having space [ New Folder ] it doesn't work. please help...
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, January 22, 2011 12:42 AM
The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ...
str = "C:\\Windows";
When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".Watched code never compiles.
-
The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ...
str = "C:\\Windows";
When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".Watched code never compiles.
Of course
'\'
is the onlyC/C++
escape character for literal strings. :)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] -
The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ...
str = "C:\\Windows";
When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".Watched code never compiles.
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}void IsFolderExists(LPTSTR str)
{WIN32\_FIND\_DATA wfd; HANDLE hFile; hFile = FindFirstFile(str,&wfd); if ( hFile != INVALID\_HANDLE\_VALUE ) { if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0) MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK); else MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK); } else { MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK); }
}
When i pass the Folder name from command line like, myapp.exe C:\Windows -result is neither file or folder but when used as myapp.exe C:\\Windows -result is Is Directory. How to solve this. Help.
Some Day I Will Prove MySelf :: GOLD
-
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}void IsFolderExists(LPTSTR str)
{WIN32\_FIND\_DATA wfd; HANDLE hFile; hFile = FindFirstFile(str,&wfd); if ( hFile != INVALID\_HANDLE\_VALUE ) { if ((wfd.dwFileAttributes & FILE\_ATTRIBUTE\_DIRECTORY)!=0) MessageBox(NULL,\_T("Is Directory"),\_T(""),MB\_OK); else MessageBox(NULL,\_T("Is File"),\_T(""),MB\_OK); } else { MessageBox(NULL,\_T("Neither File or Folder"),\_T(""),MB\_OK); }
}
When i pass the Folder name from command line like, myapp.exe C:\Windows -result is neither file or folder but when used as myapp.exe C:\\Windows -result is Is Directory. How to solve this. Help.
Some Day I Will Prove MySelf :: GOLD
-
The "\" is known as the (or one of?) "escape" character. that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes) so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something. the question is how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ...
str = "C:\\Windows";
When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".Watched code never compiles.
how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... s t r = " C : \ \ W i n d o w s " ; i agree with you. But i want to do it programatically. I want to replace \ with ''\\''. Please help
Some Day I Will Prove MySelf :: GOLD
-
how can I put a literal "\" in string without being recognize as an escape character ? the anwser is to double the "\" ... s t r = " C : \ \ W i n d o w s " ; i agree with you. But i want to do it programatically. I want to replace \ with ''\\''. Please help
Some Day I Will Prove MySelf :: GOLD
The
'\\'
notation is only needed for the compiler. It isn't needed in the runtime. (when you select a file from the open file dialog, the path is returned as"C:\New Folder"
If you want to replace them for other reasons, then use 4 '\'. i.e. to specify a SMB server, you would useCString strServer = "\\\\server_name\\folder";
. To replace it you just need a replace function. If you are using something liksCString
, it as aReplace()
method.CString strName = "\\hello";
strName.Replace("\\", "\\\\");
//strName is now "\\hello" if it were printed to screen, or "\\\\hello" from the compilers view -
The
'\\'
notation is only needed for the compiler. It isn't needed in the runtime. (when you select a file from the open file dialog, the path is returned as"C:\New Folder"
If you want to replace them for other reasons, then use 4 '\'. i.e. to specify a SMB server, you would useCString strServer = "\\\\server_name\\folder";
. To replace it you just need a replace function. If you are using something liksCString
, it as aReplace()
method.CString strName = "\\hello";
strName.Replace("\\", "\\\\");
//strName is now "\\hello" if it were printed to screen, or "\\\\hello" from the compilers viewThanx a lot.:thumbsup:
Some Day I Will Prove MySelf :: GOLD