Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c++ - How do I recursively create a folder in Win32?

I'm trying to create a function that takes the name of a directory (C:fooar, or ..fooar..az, or \someserverfooar), and creates directories as necessary so that the whole path is created.

I am attempting a pretty naive implementation of this myself and it seems to be a string processing nightmare. There is / vs , there is the special case of network shares which begin with \ (also you can't attempt to mkdir() the first two levels of the path which are machine name and share name), and there is . type nonsense that can exist in a path.

Does there exist a simple way to do this in C++?

question from:https://stackoverflow.com/questions/1530760/how-do-i-recursively-create-a-folder-in-win32

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note: this answer is somewhat quick and dirty, and doesn't handle all cases. If this is ok with you, read on. If not, consider using one of the other options.


You can use good old mkdir for that. Just run

system("mkdir " + strPath);

and you're done.

Well, almost. There are still cases you have to take care of, such as network shares (which might not work) and backslashes. But when using relatively safe paths, you can use this shorter form.

Another thing you might find useful getting rid of the possible nuisance is _fullpath(), which will resolve the given path into a full and clean one. Knowing you have a clean path, you should have no problem writing a rather trivial recursive function that will create the folders one by one, even when dealing with UNC paths.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...