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

查看:109
本文介绍了如何递归地在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天全站免登陆