C++:用CreateDirectory创建文件目录

C++ 创建多级目录文件 创建文件目录 阅读详情
AI摘要

本文详细介绍了CreateDirectory函数的使用方法,包括如何创建单级目录、处理路径不存在的情况以及如何检查目录是否存在。此外还提供了示例代码说明如何实现多级目录的创建。

CreateDirectory这个函数的作用是创建一个新的目录。如果底层文件系统支持文件和目录上的安全描述,该功能可将指定的安全描述到新的目录。

函数原型:
BOOL CreateDirectory(
  LPCTSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

参数:
    pPathName:长指针,指向一个空结束的字符串,该字符串指定要创建的目录的路径。有一个默认的字符串大小限制为MAX_PATH字符的路径。此限制是关系到这个函数是如何解析路径。字符串的长度不超过MAX_PATH。

    lpSecurityAttributes:忽略,一般设置为NULL。

返回值:
    非零表示成功,零表示失败。若想获得更多的错误信息,调用GetLastError函数。

备注:
    这个函数不是递归的。它可以在一个路径中创建唯一的最终目录。也就是说,如果父目录或中间目录不存在,该函数将失败并显示错误消息ERROR_PATH_NOT_FOUND。

    一些文件系统,如NTFS文件系统,支持压缩或个别文件和目录加密。上卷格式化为这样的文件系统,一个新的目录继承父目录的压缩和加密属性。

--->>>

    从备注可知,该函数只能创建一级目录,当根目录或者中间目录不存在时,该函数将不起作用。所以假如我们要创建二级以上目录时,应该分开来一步步创建。

例如:我们要创建:D:\\一级文件夹\\二级文件夹\\...
CString str = "D:\\一级文件夹";
CreateDirectory(str, NULL);
str += "\\二级文件夹";
CreateDirectory(str, NULL);
以此类推。

在创建新的目录之前,我们也可以先判断该目录是否已经存在,例如:

CString str = "D:\\一级文件夹";
if (!PathIsDirectory(str))
{

    ::CreateDirectory(str, NULL);

}

str += "\\二级文件夹";
if (!PathIsDirectory(str))
{

    ::CreateDirectory(str, NULL);

}

在调用PathIsDirectory()函数时,需要包含以下头文件与库函数:

#include "shlwapi.h"

#pragma comment(lib,"shlwapi.lib")

目录生成 CreateDirectory 文件夹操作实例 string directory; string direlist=this.TXTList.Text; string[] txtLines = direlist.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); foreach (string str in txtLines) { directory=this.TXTDirectory.Text+'\\' +str; if (!Directory.Exists(@directory)) { 立即下载

相关推荐

MFC 创建文件目录

递归创建文件目录,很简单,很实用,至于你信不信,反正我是信了

CreateDirectory的使用

CreateDirectory这个函数的作用是创建一个新的目录。如果底层文件系统支持文件和目录上的安全描述,该功能可将指定的安全描述到新的目录。 函数原型: BOOL CreateDirectory(   LPCTSTR lpPathName,   LPSECURITY_ATTRIBUTES lpSecurityAttributes ); 参数:    pPathName:长指针,指

sunnyli3166的专栏 1万+

grpc安装与简单demo测试

grpc安装: 由于工作需要,最近在学google的rpc开源框架;由于本人菜鸟一枚,而且是从Windows开发转到Linux;由于之前版本控制使用的是svn,没有使用过git,所以从GitHub上下载代码是通过浏览器下载zip格式的压缩文件,折腾了好几天了,发现了各种坑,各种缺少库,各种不知道怎么把这个库给装机器上。对于一个不熟悉Linux的人来说,这个体验简直糟糕透了。没办法了又在机器上装了...

old__captain的博客 4838

c++两种创建文件夹和路径的方法

C++语言创建文件夹,CreateDirectory函数和mkdir两种方法

xiaofenzhudaya的博客 7774

自动驾驶必备:5分钟搞懂gPTP如何实现μs级时间同步(附开源代码)

本文深入解析了gPTP协议在自动驾驶系统中实现微秒级时间同步的核心机制与工程实践。针对多传感器融合与跨域协同的严苛需求,文章拆解了gPTP的频率同步、对等延迟测量与时间偏移校正三步流程,并提供了基于linuxptp和AVnu gPTP两大开源代码库的实战配置指南与性能评估方法,助力开发者构建高精度、高可靠的车载时间同步网络。

weixin_29211309的博客 175

告别生硬贴图:在UE里用网格体绘制混合多个Megascans材质,打造无缝过渡的地形细节

本文详细介绍了在虚幻引擎(UE)中使用网格体绘制技术混合多个Megascans材质的方法,帮助开发者告别生硬贴图,打造无缝过渡的地形细节。通过顶点绘制和材质混合系统,可以创建自然的地表材质过渡,如泥土、苔藓和积水效果,显著提升场景真实感。

weixin_30825581的博客 530

改造CreateDirectory函数,创建多层目录

转载出处:http://mustknow.blog.51cto.com/717921/755927 在VC中要创建目录时,常用CreateDirectory这个API,但这个API有点弱处,即不能创建深层次的目录,只能一级一级的创建。看到有人写的代码里面,凡是需要这个函数的地方,不断的查找"\\",不断的调用CreateDirectory,感觉有点纠结,代码重复太多。何不自定义一个函数,能递

chenwk891的专栏 1642

反激开关电源变压器设计实例讲解(定明芳)

内容来自《反激变压器的设计》(定明芳主讲)。 定名芳老师主要讲了10步,最后应该还有一步,线径的选择已经绕制方式。这里进行了记录整理,把一些公式来源进行了推导。所有的设计都是基于理论计算。 1.确定电源规格 2.确定工作频率和最大占空比 3.计算匝比(利用伏秒平衡原则) 此处插入解释一下伏秒平衡,根源是磁通量平衡。该原则可以用来检查MOS管耐压是否够。 4.计算初级线圈峰值电流 基于CCM模式计算的。ip1是开关管要关闭时的峰值电流,ip2是开关管开始导通时的起始电流。因为时CCM模式,所以,ip

ydgd118的博客 5万+

C++ CreateDirectory使用心得

在使用CreateDirectory()创建文件目录的时候,自己犯了一个不容易发现的错误,目录层级过多,创建失败: 例如,需求在C盘中创建一个Testlog目录,然后在Testlog中再创建一个以年月日命名的文件,自己的做法是: if (!PathIsDirectory(“C:\Testlog\2018-07-27”)) { int iRet = 0...

long1_fengshenzhilu的博客 7225

智能体在车联网中的应用:第19天 智能体理论与初步实践 D30:阶段小结——在SUMO中使用TraCI控制预编程智能体

本文介绍了如何将强化学习从算法仿真迁移到交通系统实践,重点讲解了SUMO微观交通仿真平台与TraCI实时控制接口的架构。通过构建一个预编程驾驶智能体,演示了车辆从起点到目标点的导航控制。文章详细解析了TraCI的双向通信机制和事件驱动模型,对比了其与传统RL环境的差异,并提供了完整的Python实现框架,包括路网配置、车辆控制和基于向量场的导航逻辑。该实践为后续开发高级学习型交通智能体奠定了基础。

IT深耕十余载,大道之简 1490

利用CAT12做皮层指标的统计分析及可视化笔记记录

利用CAT12做皮层指标的统计分析及可视化 文章目录利用CAT12做皮层指标的统计分析及可视化前言一、统计步骤1.Basic Models2.Estimate Surface Models3.Results二、可视化1.t值可视化2.p值可视化 前言 本来没有打算做SBM的,因为自己是新手小白,许多操作都不太懂,但最后还是打算做一下,于是就从上周末开始将很久之前跑VBM得到的surf文件夹中的数据捡起来继续做皮层分析,因为之前看了一个博主的博客,关于如何做SBM已经讲得非常清楚了,但是在统计及可视化这里,我

weixin_52073820的博客 8103

CreateDirectory创建多级目录

目的:实现用CreateDirectory创建多级目录1.引入静态库#include "shlwapi.h"#pragma comment(lib,"shlwapi.lib")2.函数const wchar_t* wcstrrchr(const wchar_t* str, const wchar_t wc) { const wchar_t* pwc = NULL; for (int i=wcs...

whish1994的博客 6429

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

c++新建删除文件夹方法CreateDirectory RemoveDirectory

遇到一个问题就是从注册表读取到数据存储路径后,由于所调用的接口只能设置已存在的文件夹,所以就想着在读取到路径之后,就先创建文件夹,然后调用接口就没问题了。 找到的方法如下: CreateDirectory Creates a new directory. If the underlying file system supports security on files and direct

thdxs的专栏 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

如何创建空文件夹CreateDirectory

//CString strPath = _T("C:\\test"); WCHAR  strPath[] =_T("C:\\test"); if (!::CreateDirectory(strPath,NULL))   //创建空文件夹 { DWORD nErro = ::GetLastError(); //AfxMessageBox(_T("创建文件夹失败")); ::Message

杰克东的专栏 2244

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

封装CreateDirectory()函数,同时创建多级目录

我们都知道CreateDirectory()函数创建目录只能一级一级的创建,中间层级不存在,后面的目录也创建不成功,先封装了函数来同时创建多级目录: BOOL CreateMultiDir(TCHAR* szPathDir) { TCHAR szPath[MAX_PATH] = {0}; TCHAR szbuf[MAX_PATH] = {0}; BOOL bFlag = FALSE; ...

小米的修行之路 3833

C++ CreateDirectory()

关于GetSystemDirectory function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx 以下代码参考:http://blog.csdn.net/co_diy/article/details/6831701,不过他好像不是用C++。 IDE: Microsoft...

qq_29761395的博客 1074

C++及Qt中创建文件夹的方法经验总结

C++及Qt中创建文件夹的方法经验总结window中用C++创建文件夹的方法1.利用system函数调用DOS命令2.CreateDirectory()函数3.Qt独有的创建文件夹的方法待解决问题1.window中用C++创建文件夹的方法 以是几种证实有效的方法。 1.利用system函数调用DOS命令 C++中的system函数 是可以调用一些DOS命令,也就是像DOS和Linux哪像发送一条命...

weixin_41615163的博客 2956

CreateDirectory和RemoveDirectory函数

在信息爆炸的年代里,把各种信息分类已经是一种非常必要的功能,比如把股票行情数据保存到硬盘里,就需要分开几个目录保存。比如你写LOG到硬盘时,也需要分成几个目录来保存,这样让维护人员很好地找到出错的信息,或者有用的信息。像我在开发银行的信用卡系统时,就需要把所有通过网络传送的数据全部生成文本文件保存到当日的目录里,也就是说每天都需要创建一个目录,把所有交易的数据生成LOG保存进去。那么面对这样的需求

芝麻软件工作室的专栏 3540

c++_CreateDirectory

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34153893的博客 1289
上一篇: 在WinForm应用程序中实现自动升级
下一篇: MFC中启动EXE
ccfxue
博客等级 码龄11年 111粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值