CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2004
    Posts
    59

    Angry 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.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185
    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.

  3. #3
    Join Date
    Jul 2004
    Posts
    59
    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?

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176
    '\' 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."

  5. #5
    Join Date
    Jul 2003
    Location
    Korea
    Posts
    60
    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

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    Quote 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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176
    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."

  8. #8
    Join Date
    Jul 2004
    Posts
    59
    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
  •  





Click Here to Expand Forum to Full Width

Featured