mkdir and multiple directories

This is a discussion on mkdir and multiple directories within the C Programming forums, part of the General Programming Boards category; It just came to my attention that mkdir won't accept parameters such as "c:/firstdir/seconddir" for creating "firstdir" in the root ...

  1. #1
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Question mkdir and multiple directories

    It just came to my attention that mkdir won't accept parameters such as "c:/firstdir/seconddir" for creating "firstdir" in the root and "seconddir" under "firstdir".
    Of course I can create individual directories like so:
    Code:
    mkdir ("c:/firstdir");
    mkdir ("c:/firstdir/seconddir");
    However, isn't there any tad more convenient solution?

    The "problem" I'm facing is when the user of my program decides to extract files to some sub-directory, it'll crash if he provides a path such as "c:/blah/subdir" and the program has to create both directories.
    I had thought of simply splitting the directory string at the forward or backward slashes, etc., but wasn't sure if there would be some niftier solution, eh.
    Chaos, panic, pandemonium... My work here is done!

  2. #2
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Red face

    Well, one of my "greatest" weakness when it comes to programming are string manipulations. Even the most basic ones seem to get me all puzzled.

    Code:
    void _mkpath (char *path)
    {
    	char	*s, *dir = NULL;
    
    	s = path + strlen (path) - 1;
    
    	while (s != path && *s != '/' && *s != '\\')
    	{
    		s--;
    	}
    
    	strncpy (dir, path, s - path);
    	dir[s - path] = 0;
    	mkdir (dir);
    }
    Scrap that, it's useless. Any example of doing what I've described in my first message? I'm not asking you to write the complete function for me, but rather show me an example. As I said, strings are one of my weaknesses.
    Chaos, panic, pandemonium... My work here is done!

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: mkdir and multiple directories

    Originally posted by Idle
    It just came to my attention that mkdir won't accept parameters such as "c:/firstdir/seconddir" for creating "firstdir" in the root and "seconddir" under "firstdir".
    Of course I can create individual directories like so:
    Code:
    mkdir ("c:/firstdir");
    mkdir ("c:/firstdir/seconddir");
    However, isn't there any tad more convenient solution?

    The "problem" I'm facing is when the user of my program decides to extract files to some sub-directory, it'll crash if he provides a path such as "c:/blah/subdir" and the program has to create both directories.

    I had thought of simply splitting the directory string at the forward or backward slashes, etc., but wasn't sure if there would be some niftier solution, eh.
    Nope, you gotta createboth dirs individually.

    One way is to use a recursive function:
    Code:
    void  makedir(char *dir)
    {
        /* attempt to use mkdir */
        /* if error,  */
            /* copy dir string up to last  /  */
            /* call makedir with the new string */
        /* else  */
            /* return  */
    
        /* returned here -- use mkdir again */
         /* return  */
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Lightbulb

    It's the string stuff that's getting to me, rather than the theory or logics, hrm.
    I thought I'd be making some progress here... Now, let's see:
    Code:
    	p = strchr (path, '/');
    	while (*p)
    	{
    		if ((*p == '/') || (*p == '\\'))
    			printf ("p = %s\n", p);
    
    		*p = 0, ++p;
    	}
    This, given 'path' of 'c:/one/two' prints '/one/two' and '/two'. Closer to what I'm looking for, but not quite "it", however. I believe this has been done many times before, and someone must be able to show me an example - I don't think I should have to resort to "C for dummies" or similar readings.

    I've been doing this for years (programming, that is), and never have I been able to fully understand nor seem to "get the hang of" these string tricks, it's rather sad.
    Chaos, panic, pandemonium... My work here is done!

  5. #5
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Post

    I'll probably find my answer in the GNU fileutils package. Thanks.
    Chaos, panic, pandemonium... My work here is done!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21