Multiple directory creation.

Jan 4, 2016 at 12:05pm
Playing around with file streams and directory creations I stumbled upon the _mkdir command and wanted to try something. I wanted to stick that command inside a for loop and create 10 folders when ran. For obvious reasons the OS (Windows) can't have duplicated folder names so the following code only generates 1 folder named "testfolder".

Using the code below how hard would it be to generate 10 folders assigned testfolder1, testfolder2, testfolder3, etc, etc?

Also, is mkdir the best command for this? I guess if all I am trying to do is generate a bunch of empty folders at runtime then use whatever gets the job done, just curious if there is a more updated or "proper" way of generating directories, maybe thru the use of the WINAPI or something.

1
2
3
4
5
6
7
8
9
10
11
#include <direct.h>

int main()
{
	for(int i = 0; i < 10; i++)
	{
		_mkdir("C:\\testfolder");
	}

return(0);
}
Last edited on Jan 4, 2016 at 12:08pm
Jan 4, 2016 at 12:12pm
It's no problem at all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <direct.h>
#include <stdio.h>

int main()
{
  char buffer[255];

  for(int i = 0; i < 10; i++)
  {
    sprintf(buffer, "C:\\testfolder%d", i);
    _mkdir(buffer);
  }
  return(0);
}

Jan 4, 2016 at 12:18pm
Indeed that works. Now I'm trying to figure out exactly why or how it works, heh.

EDIT: I think I understand now. It's storing the loop in a character buffer and generating folders off that. Thanks!
Last edited on Jan 4, 2016 at 12:24pm
Jan 4, 2016 at 1:01pm
Also, is mkdir the best command for this?

if you don't care about portability then probably yes, otherwise you can also use boost:
boost::filesystem::create_directory
Topic archived. No new replies allowed.