首页 > 系统相关 >C++ 使用Windows的API CreateDirectory 创建多层级文件夹

C++ 使用Windows的API CreateDirectory 创建多层级文件夹

时间:2023-11-28 10:03:59浏览次数:29  
标签:std paths string Windows pos C++ API path include


简介

使用Windows的API创建多层级文件夹

效果

C++ 使用Windows的API CreateDirectory 创建多层级文件夹_ide

代码

#include <windows.h>  
#include <direct.h>  
#include <iostream>  
#include <string>  
#include <sstream>  
#include <vector>  

//创建多层级文件夹
bool CreateDir(const std::string& path)
{
	std::vector<std::string> paths;
	std::string current_dir;
	size_t start_pos = 0;
	size_t end_pos;
	while ((end_pos = path.find_first_of("\\/", start_pos)) != std::string::npos)
	{
		current_dir = path.substr(0, end_pos);
		paths.emplace_back(current_dir);
		start_pos = end_pos + 1;
	}
	paths.emplace_back(path);
	paths.emplace_back(path);//第二次创建会返回“目录已存在”的错误,即创建文件夹成功!

	for (auto ph : paths)
	{
		int num = MultiByteToWideChar(CP_ACP, 0, ph.c_str(), -1, NULL, 0);
		wchar_t* wide = new wchar_t[num];
		MultiByteToWideChar(CP_ACP, 0, ph.c_str(), -1, wide, num);
		std::wstring wcpath(wide);
		delete[] wide;

		CreateDirectory(wcpath.c_str(), NULL);
	}

	DWORD error = GetLastError();
	if (error == ERROR_ALREADY_EXISTS)//目录已存在
		return true; 
	else
		return false;
}

int main()
{
	std::string path = "E:\\li\\123/可行\\abc";
	bool ret = CreateDir(path);
	if (ret)
		std::cout << path << std::endl << "创建成功!" << std::endl;
	std::cin.get();
	return 0;
}

输出:
E:\li\123/可行\abc
创建成功!


标签:std,paths,string,Windows,pos,C++,API,path,include
From: https://blog.51cto.com/wangpaifeixingy/8595254

相关文章

  • C++ 获取文件创建时间、修改时间、大小等属性
    简介获取文件创建时间、修改时间、大小等属性代码#include<iostream>#include<string.h>#include<time.h>voidmain(){std::stringfilename="E:\\LiHai123.txt";struct_statstat_buffer;intresult=_stat(filename.c_str(),&stat_b......
  • C++ 获取网卡名称和IP地址
    描述这是获取网卡名称和IP地址的代码示例,参考自。原文描述得比较详细,感谢博主分享。原文代码中没有输出网卡的物理地址,下面的代码进行了补充,并在win10上运行正常。代码//#include<WinSock2.h>#include<Iphlpapi.h>#pragmacomment(lib,"Iphlpapi.lib")//需要添加Iphlpapi.lib......
  • C\C++ 设置Visual Studio编译器使用C++17标准
    文章作者:里海简介:        使用ISOC++17标准可以为开发人员带来许多好处,包括更简洁的代码、更高的运行效率、更好的硬件支持、更好的兼容性和可移植性,以及更好的多线程编程支持等。那么如何设置vs使用c++标准呢?下面是方法。注意需要vs2017及以上版本。方法:打开VisualStud......
  • C++ 33.C++中的字符串类-狄泰软件学院
    C语言字符串的历史C语言不支持真正意义上的字符串C语言用字符数组和一组函数实现字符串操作C语言不支持自定义类型,因此无法创建字符串类型当年C语言主要用于开发UNIX操作系统,处理字符串的情况少,所以在当时的背景下没有让C语言中内置一个字符串类型。后来C语言越用越广泛,没办法只能......
  • C\C++ 专栏目录
    个人总结序号内容笔记01C++获取网卡名称和IP地址笔记链接02C++设置VisualStudio编译器使用C++17标准笔记链接03C++使用Pugixml库,轻松处理XML文件笔记链接04C++使用ShellExecuteEx调exe程序笔记链接05C++使用exception类,抛出自定义异常并捕获笔记链接06C++使用soc......
  • C++标准库类std::packaged_task
    std::packaged_task是C++11引入的标准库类,用于封装可调用对象,如函数等,并将封装对象作为异步任务进行管理,通过与std::future结合使用,完成异步任务结果的获取。#include<iostream>#include<thread>#include<future>std::stringpromise_string(std::stringstr){......
  • 从前端的角度来梳理微信支付(小程序、H5、JSAPI)的流程
    因业务需要,开发微信支付功能,涉及三种支付方式:JSAPI支付:微信内网页支付,需要开通微信服务号小程序支付:在小程序中支付,需要开通小程序H5支付:在手机浏览器(出微信内网爷)中网页支付使用微信支付的前提必开通微信商户号,要使用到那种的支付方式要前需在商户平台开通(要审核)。支付......
  • windows edge浏览器免费复制网页文字
      复制时,出现上面提示时候  使用edge浏览器打开链接,在http前面加入read:,然后打开,即可复制 ......
  • 实验4 现代C++标准库与类模板
    实验任务5:1.代码:textcoder.hpp:1#pragmaonce23#include<iostream>4#include<vector>5#include<array>6#include<string>7usingnamespacestd;89classTextCoder10{11private:12stringtext;13......
  • 【windows常见问题】windows远程桌面授权许可证过期解决办法
    title:windows远程桌面授权许可证过期解决办法(之一)date:2022-02-0910:54:06updated:2022-02-0914:20:00tags:-Aliyun-Windows-mstsccategories:-windowsdescription:windows远程桌面授权许可证过期abbrlink:'use-windows2019-to-Resolve-remote-......