如何在Win32中递归创建文件夹? [英] How do I recursively create a folder in Win32?

查看:177
本文介绍了如何在Win32中递归创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用目录名称的函数( C:\foo\bar .. \foo\bar\..\baz \\someserver\foo\bar ),并创建目录,以便创建整个路径。

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

我正在尝试一个非常幼稚的实现这个我自己,这似乎是一个字符串处理噩梦。有 / vs \ ,网络共享的特殊情况以 \\ (你也不能尝试mkdir()路径的前两个级别,它们是机器名和共享名),还有 \。 \ 键入可以存在于路径中的废话。

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.

在C ++中是否存在一种简单的方法?

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

推荐答案

注意:这个答案有点快,肮脏,并不处理所有的情况。如果这可以和你同在,请继续阅读。如果没有,请考虑使用其他选项之一。

您可以使用好的旧的mkdir。只需运行

You can use good old mkdir for that. Just run

system("mkdir " + strPath);

你已经完成了。

好吧,差不多您仍然需要处理的情况,例如网络共享(可能无法使用)和反斜杠。但是当使用相对安全的路径时,您可以使用这种较短的形式。

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.

另一件可能会有用的消除可能的麻烦是 _ fullpath(),这将将给定的路径解析为一个完整而干净的路径。知道你有一个干净的路径,你应该没有问题编写一个相当琐碎的递归函数,即使在处理UNC路径时也会逐个创建文件夹。

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.

这篇关于如何在Win32中递归创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆