7

I need a script that can create a sub folders automaticaly.

For example:

I have a base folder stored at c:/upload. I tranfer to script folders string: /2011/23/12/3. Script should parse this string and create folders and subfolders in c:/upload like string is (should be c:/upload/2011/23/12/3)

How can I make this?

Now I use if/else and check if folder/subfolder exist, but the script is tooooo big and that is hard to manage.

1
  • What is the last folder? I'm referring to the number three. Seems like you basically use c:\upload\yyyy\MM\dd Commented May 27, 2011 at 7:31

3 Answers 3

18

Have you looked at Directory.CreateDirectory, which will create any missing directories along the way?

From the documentation:

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid.

Sign up to request clarification or add additional context in comments.

4 Comments

all directories in the path that not exist yet will be created? for example Directory.CreateDirectory("c:/upload/2011/23/12/3") where 23,12,3 not exist will be created using this function?
@evgeniy.labusnkiy As easy as test it :)
@evgeniy: Yes. Read the documentation I linked to: "Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. "
realy, i dont know that this is so simple :)))
6

Your code to create directories recursively will be as simple as:

Directory.CreateDirectory(path)

4 Comments

@Oskar Kjellin: I didnt notice the asp.net-mvc tag earlier. However, the method call to do this remains the same.
indeed it does. That's basically what @Jon wrote as well. I only objected to creating a console app
Please, at least test your code before posting it. Where is the overload that receives a bool?
Darn it. Two in a row :(. That's what happens when you answer a question which @Jon Skeet has already answered.
1

You can use Directory.CreateDirectory in C# for creating directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.