|
-
August 22nd, 2004, 04:29 PM
#1
How to Create Directory and Subdirectory?
I want to create directories using CString variable "C:\backup\drivers" from an Edit Box.
Using CreateDirectory() only works with "c:\backup".
Some functions I found can create both directory and subdircetory only with CString such as "c:\\backup\\drivers", but will not work with "C:\backup\drivers".
Another function in my application only works with "C:\backup\drivers", but not with "c:\\backup\\drivers".
Is there any function which can create "C:\backup\drivers"?
Thanks a lot.
-
August 22nd, 2004, 04:51 PM
#2
then use yourstring.Replace("\\", "\\\\") to make it two slashes
you should use CreateDirectory directory by directory:
Code:
while (yourstring.Find("\\") != -1)
{
// Create the next directory until the next slash
}
Last edited by Tischnoetentoet; August 22nd, 2004 at 04:53 PM.
-
August 22nd, 2004, 07:16 PM
#3
Thanks for your suggestion. I tried the code, but it doesn't work.
Here is my code.
m_loct is CString Variable, c:\backup\drivers in an EDit Box.
CString dir;
dir = m_loct;
dir.Replace("\\", "\\\\");
WriteDirectory(dir);
But it doesn't work. IF I just use the following code:
CString dir="C:\\backup\\drivers";
WriteDirectory(dir);
both directories were created.
I test after the code dir.Replace("\\", "\\\\");
I found dir is C:\\backup\\drivers
But directory was not created.
What's wrong?
-
August 22nd, 2004, 07:59 PM
#4
'\' symbol is a command character IN C/C++ LANGUAGES that is used to set some unwritable characters- SINGLE CHARACTERS- like '\n'. To set a SINGLE CHARACTER '\' you should write in C/C++ double '\', and in the resulted string, for example, "\\", there would be only one '\'. Try to out this string ans look at this. So, if you write a path IN SOURCE FILE using double '\', you are really writing path with single '\''s. If you write in source single '\', you are likely to do a mistake, because the '\' characters and the following ones would be some unusual single characters.
ghm...
So, If you will implement some input, user would enter for example "C:\myfile.ext", you will receive the same result as if you declarated this string in your source file like this:
Code:
char *str="C:\\myfile.ext"
"Programs must be written for people to read, and only incidentally for machines to execute."
-
August 22nd, 2004, 08:37 PM
#5
There is another way, you can use Shell function: SHCreateDirectoryEx. You do not need create directories from outermost to innermost. This function automatically creates parents directory if they do not exist.
Quang
-
August 23rd, 2004, 02:20 AM
#6
 Originally Posted by Apal
CString dir;
dir = m_loct;
dir.Replace("\\", "\\\\");
WriteDirectory(dir);
There is no such function as WriteDirectory? Where did you get this function? Did you write it? Better use quangnt suggestion.
-
August 23rd, 2004, 05:18 AM
#7
Anyway using smth like dir.Replace("\\", "\\\\"); is wrong in principle...
"Programs must be written for people to read, and only incidentally for machines to execute."
-
August 23rd, 2004, 07:30 PM
#8
Guys, Thank you very much for your replies.
I finally got it. Now it works well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|