下面给出创建单个文件夹的方法,每一种方法后面都紧跟着对应的删除文件夹的方法。
此处参考博主。

一:调用Windows API函数 CreateDirectory()和 RemoveDirectory(),成功返回0,否则返回非零。

头文件<windows.h>

创建:CreateDirectory()

#include <Windows.h>   //头文件
#include<iostream>
using namespace std;int main()
{string path = "E:\\1";bool flag = CreateDirectory(path.c_str(), NULL);return 0;
}

删除: RemoveDirectory()

#include <iostream>
#include <Windows.h>   //头文件
using namespace std;int main()
{string path = "E:\\1";bool flag = RemoveDirectory(path.c_str());return 0;
}

二. 调用C运行库函数int mkdir()和int rmdir(),返回0表示成功,-1失败

头文件<direct.h>

创建:mkdir()或 ::_mkdir()

没有下划线的位不符合ISO c++ 标准的写法,标准要求带下划线的标准,没有下划线的是为了兼容以前的版本。

#include<direct.h>    //头文件
#include<iostream>
using namespace std;int main()
{string path = "D:\\1";mkdir(path.c_str());return 0;
}

删除:rmdir()或 ::_rmdir()

#include<direct.h>    //头文件
#include<iostream>
using namespace std;int main()
{string path = "D:\\1";rmdir(path.c_str());return 0;
}

三.调用system命令md 和 rd

创建:

#include<iostream>
using namespace std;int main()
{system("md D:\\1");system("pause");//屏幕暂停return 0;
}

删除:

#include<iostream>
using namespace std;int main()
{system("rd D:\\1");system("pause");//屏幕暂停return 0;
}

四:检查文件是否存在

使用access()函数,包含头文件<io.h>

int access(const char *filenpath, int mode);

amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。
这个函数还可以检查其它文件属性:
06 检查读写权限
04 检查读权限
02 检查写权限
01 检查执行权限
00 检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1

#include<direct.h>
#include<io.h>
#include<iostream>  using namespace std;
int main()
{string path = "D:\\test1";if (access(path.c_str(), 0) == -1)//返回值为-1,表示不存在{printf("不存在,创建一个\n");int i = mkdir(path.c_str());}return 0;
}

五:删除文件

C/C++ 删除文件 remove函数

头文件:
#include <stdio.h> //C
#include < cstdio > //C++

函数原型:int remove(const char * filename);

返回结果:如果成功返回 0,失败返回“EOF”( -1)。

#include<iostream>
#include<cstdio>using namespace std;int main()
{char *savePath = "D:\\test1.txt";if(remove(savePath)==0){cout<<"删除成功"<<endl;}else{cout<<"删除失败"<<endl;}return 0;
}

DeleteFile,可以用此参数删除指定文件。

BOOL DeleteFile(
LPCSTRlpFileName//要删除的文件名的指针
);
成功返回非零,失败返回0

int main()
{CString savePath = "D:\\test1.txt";SetFileAttributes(savePath , FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性if(DeleteFile(savePath)){cout<<"删除成功"<<endl;}else{cout<<"删除失败"<<endl;}return 0;
}

CopyFile,可以用此参数拷贝指定文件。

copyfile(
lpcstr lpexistingfilename, //源文件路径
lpcstr lpnewfilename, //新文件路径
bool bfailifexists //为true的话,如果新文件已存在, 则返回false; 为false的话,如果新文件已存在,会将原文件覆盖。
);

函数成功返回true,失败返回false;

CopyFile("C:\\File1.txt","C:\\File2.txt",TRUE);

MoveFile,可以用此参数改指定文件的文件名。

BOOL MoveFile(
LPCTSTR lpExistingFileName, // file name
LPCTSTR lpNewFileName // new file name
);

MoveFile("C:\\File1.txt","C:\\File3.txt");

六:createDirectory 多级创建文件夹

#include "windows.h"
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;bool createDirectory(std::string folder)
{std::string folder_builder;std::string sub;sub.reserve(folder.size());for (auto it = folder.begin(); it != folder.end(); ++it) {//cout << *(folder.end()-1) << endl;const char c = *it;sub.push_back(c);if (c == PATH_DELIMITER || it == folder.end() - 1) {folder_builder.append(sub);if (0 != ::_access(folder_builder.c_str(), 0)) {// this folder not existif (0 != ::_mkdir(folder_builder.c_str())) {// create failedreturn false;}}sub.clear();}}return true;
}

七:DeleteDirectory多级删除文件夹

#include "windows.h"
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;bool DeleteDirectory(CString DirName)
{CString PUBPATH;PUBPATH = DirName;CFileFind tempFind;DirName += "\\*.*";BOOL IsFinded = (BOOL)tempFind.FindFile(DirName);//cout << IsFinded <<endl;while (IsFinded){IsFinded = (BOOL)tempFind.FindNextFile();if (!tempFind.IsDots()){CString strDirName;strDirName += PUBPATH;strDirName += "\\";strDirName += tempFind.GetFileName();if (tempFind.IsDirectory()){DeleteDirectory(strDirName);}else{SetFileAttributes(strDirName, FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性DeleteFile(strDirName);}}}tempFind.Close();if (!RemoveDirectory(PUBPATH)){return false;}return true;}

八:多级创建文件夹(删除已有的文件夹里所有内容)

只需要将七和八结合起来。

#include "windows.h"
#include <iostream>
#include <io.h>
#include <direct.h>
using namespace std;bool createDirectory(std::string folder)
{std::string folder_builder;std::string sub;sub.reserve(folder.size());for (auto it = folder.begin(); it != folder.end(); ++it) {//cout << *(folder.end()-1) << endl;const char c = *it;sub.push_back(c);if (c == PATH_DELIMITER || it == folder.end() - 1) {folder_builder.append(sub);if (0 != ::_access(folder_builder.c_str(), 0)) {// this folder not existif (0 != ::_mkdir(folder_builder.c_str())) {// create failedreturn false;}}else{if (it == folder.end() - 1)//新的文件夹{    //先删除里面的内容if (TRUE != DeleteDirectory(folder_builder.c_str())) {return false;}if (0 != ::_mkdir(folder_builder.c_str())) {// create failedreturn false;}}}sub.clear();}}return true;
}

C/C++创建和删除文件夹操作(包含多级)相关推荐

  1. Linux 创建、删除文件夹

    Linux 创建.删除文件夹 创建文件夹 创建aa文件夹,查看列表:可以看到已经有文件夹aa; mkdir aa 批量创建文件夹(bb cc dd),查看列表:可以看到有文件夹多了三个(bb cc d ...

  2. C++文件操作——创建和删除文件夹

    创建文件夹目录的相关方法: (1).采用CreateDirectory函数 CString strPath; GetModuleFileName(NULL,strPath.GetBufferSetLe ...

  3. Linux小小白入门教程(六):创建和删除文件夹

    以下操作在Linux终端进行.Linux因为权限非常严格,所以暂时所有的命令操作全部是在/home文件夹下的/yangjw文件夹下进行./yangjw文件夹就是登录用户名所在的文件夹,出了此文件夹,命 ...

  4. 转:飝兒物語的“Linux创建、删除文件夹”

    原创地址: http://www.cnblogs.com/zf2011/archive/2011/05/17/2049155.html 创建文件夹[mkdir] 一.mkdir命令使用权限 所有用户都 ...

  5. linux下创建、删除文件夹

    1.在 Linux 下,我们可以使用 mkdir 命令创建目录,mkdir 是"make directory" 的缩写词. 运行 mkdir 命令 默认情况下,不带任何参数运行 m ...

  6. VB创建及删除文件夹

    一.创建文件夹(多级): Private Sub MakeDirPath(strDirName As String) Dim i As Long, strPath As String Do i = I ...

  7. java 创建、删除文件夹

    1.创建文件夹 publicl static String createDirectory(HttpServletRequest request){ StringBuilder sb=new Stri ...

  8. 使用node中fs模块创建和删除文件夹

    创建文件夹 假如我们要创建这样一个文件夹'a/d/c/d/e' 同步创建文件夹 let fs = require('fs'); function makep(dir) {let paths = dir ...

  9. MATLAB可以进行多种文件操作,包括读取和写入文本、二进制和其他格式的文件,创建和删除文件夹等等

    常见的文件操作方法: 读取文本文件 使用 fopen 函数打开文件,然后使用 fscanf 函数读取文件中的数据.例如 fid = fopen('myfile.txt'); data = fscanf ...

最新文章

  1. java 数组 题_(第22讲)java数组的一些编程题
  2. 运维基础(10)linux被删数据恢复方法
  3. 在Elasticsearch中查询Term Vectors词条向量信息
  4. XtraBackup全备与增量备份
  5. Java SE 8新功能介绍:使用新的DateTime API计算时间跨度
  6. linux-vim-进入编辑模式的多种方法
  7. Git协助方式:Fork项目开发新功能并使用Pull-Request把新特性推送给原项目
  8. .net链接带密码的ACCESS数据库
  9. iPhone 不能读取plist文件!?
  10. docker端口映射失效解决方法
  11. 重磅!原清华副校长任职南科大校长:他考研三次,读博七年,想做科研人偶像...
  12. 深入理解Intel Core Microarchitecture
  13. 火星时代室内效果图风暴10CD B
  14. 继电器rc吸收电路取值_RC吸收电路
  15. android+浮层布局,如何使用Android实现单页面浮层可拖动view
  16. 一款开源的支持离线的支持MarkDown的优秀笔记软件----思源笔记
  17. NAudio:MP3转WAV和Wav转Mp3
  18. 运维:你们 JAVA 服务怎么又又又又出问题了!内存降不下来!
  19. 如何获取小程序链接的配置参数
  20. C/C++ 延时函数 (标准库)

热门文章

  1. Net深入实战系列—JSON序列化那点事儿
  2. 互联网电影院超级应用诞生,打造身临其境的沉浸感
  3. Delphi 学习ClientDataSet
  4. Tomcat 端口配置文件端口说明
  5. vue3 ts main添加vform3,无法找到模块“vform3-builds”的声明文件
  6. html5 canvas 制作画图工具。
  7. 本blog初养成小记暨博客初心宣言
  8. linux 路由表设置 之 route 指令详解 -1
  9. 投资成本低且无需专人维护!华为云会议让沟通更高效
  10. Java对象数组的使用