Windows 下创建目录,以及删除目录,可以运行的




创建目录利用_mkdir
,删除目录利用_rmdir
目录是否可以访问_access
设置当前的访问目录_chdir


利用_findfirst,_findnext进行遍历整个目录,遍历结束需要关闭句柄_findclose。


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <io.h>  
  2. #include <windows.h>  
  3. #include <direct.h>  
  4. #include <sys/stat.h>  
  5. #include <string.h>  
  6. #include <stdio.h>  
  7.   
  8. bool createDirectory(const char* pathName)  
  9. {  
  10.     char path[MAX_PATH];  
  11.     memset(path, 0x00, MAX_PATH);  
  12.     const char* pos = pathName;  
  13.     while ((pos = strchr(pos, '\\')) != NULL)  
  14.     {  
  15.         memcpy(path, pathName, pos - pathName + 1);  
  16.         pos++;  
  17.         if (_access(path, 0) == 0)  
  18.         {  
  19.             continue;  
  20.         }  
  21.         else  
  22.         {  
  23.             int ret = _mkdir(path);  
  24.             if (ret == -1)  
  25.             {  
  26.                 return false;  
  27.             }  
  28.         }  
  29.     }  
  30.     return true;  
  31. }  
  32.   
  33.   
  34. bool deleteDirectory( char* pathName)  
  35. {  
  36.     struct _finddata_t fData;  
  37.     memset(&fData, 0, sizeof(fData));  
  38.   
  39.     if (_chdir(pathName) != 0) //_chdir函数设置当前目录  
  40.     {  
  41.         printf("chdir failed: %s\n",pathName);  
  42.         return false;  
  43.     }  
  44.   
  45.     intptr_t hFile = _findfirst("*",&fData);  //参数1:char *类型,"*"表示通配符,可以查找文件、文件夹  
  46.     if(hFile == -1)  
  47.     {  
  48.         printf("_findfirst error!\n");  
  49.         return false;  
  50.     }  
  51.   
  52.     do  
  53.     {  
  54.         if(fData.name[0] == '.')  
  55.             continue;  
  56.         if(fData.attrib == _A_SUBDIR) //子文件夹  
  57.         {  
  58.   
  59.             char dirPath[MAX_PATH];  
  60.             memset(dirPath,0,sizeof(pathName));  
  61.             strcpy_s(dirPath,pathName);  
  62.             strcat_s(dirPath,"\\");  
  63.             strcat_s(dirPath,fData.name);  
  64.   
  65.             deleteDirectory(dirPath);  //recursion subdir  
  66.             printf("remove dir: %s\n",dirPath);  
  67.             _chdir("..");  
  68.              _rmdir(dirPath);  
  69.         }  
  70.         else  
  71.         {  
  72.             char filePath[MAX_PATH];  
  73.             memset(filePath,0,sizeof(filePath));  
  74.             strcpy_s(filePath,pathName);  
  75.             strcat_s(filePath,"\\");  
  76.             strcat_s(filePath,fData.name);  
  77.   
  78.             remove(filePath);  
  79.             printf("remove file: %s\n",filePath);  
  80.         }  
  81.     }while(_findnext(hFile,&fData) == 0);  
  82.   
  83.     _findclose(hFile);  //close  
  84.   
  85.     return true;  
  86. }  
  87.   
  88. int main(void)  
  89. {  
  90.     createDirectory("D:\\test\\txt");  
  91.     deleteDirectory("D:\\test");  
  92.     return 0;  
  93. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值