使用方法:将.c和.cpp文件添加到你的项目中,然后#include “FileDirTool.h”
#include#include "FileDirTools.h" int main() { // //listDir使用示例 // char dirList[50][20]; // int dirCnt = listDir("Database", dirList); // for (int i = 0; i < dirCnt; ++i) // printf("%sn", dirList[i]); // //createDir使用示例 // int crF = createDir("Database"); // printf("%dn", crF); // //dropDir使用 // int drF = dropDir("Database"); // printf("%dn", drF); // //cascadeDropDir使用 // cascadeDropDir("Database"); // //listFile使用示例 // char fileList[50][20]; // int fileCnt = listFile("Database", fileList); // for (int i = 0; i < fileCnt; ++i) // printf("%sn", fileList[i]); // //createFile使用示例 // int crF = createFile("Database\test.txt"); // printf("%dn", crF); // //dropFile使用示例 // int drF = dropFile("Database\test.txt"); // printf("%dn", drF); return 0; }
//FileDirTools.h #ifndef FILEDIRTOOLS_H_INCLUDED #define FILEDIRTOOLS_H_INCLUDED #include#include #include #include //在指定路径下创建文件夹 //返回值 0创建成功 1文件夹已存在 -1不存在且创建失败 int createDir(char* path); //在指定路径下删除文件夹(里面有内容将失败返回-1) //返回值 0删除成功 1文件夹不存在 -1存在且删除失败 int dropDir(char* path); //级联删除指定路径下的文件夹以及文件夹下的普通文件 void cascadeDropDir(char* path); //返回指定路径下的所有文件夹名(上限50个目录 长度20) //path路径 dirList文件夹名列表 返回值为文件夹名列表长度 int listDir(char *path, char dirList[][20]); //创建文件 成功0失败-1 int createFile(char *path); //删除文挨 成功0失败-1 int dropFile(char *path); //返回指定路径下的所有文件名(上限50个文件 长度20) //path路径 fileList文件名列表 返回值为文件名列表长度 int listFile(char *path, char fileList[][20]); #endif // FILEDIRTOOLS_H_INCLUDED
//FileDirTools.c
#include "FileDirTools.h"
int createDir(char* path)
{
if (0 != access(path, 0))
{
return mkdir(path);
}
else
{
return 1;
}
}
int dropDir(char* path)
{
if (0 == access(path, 0))
{
return rmdir(path);
}
else
{
return 1;
}
}
void cascadeDropDir(char* path)
{
char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
strcpy(tPath, path);
tPath[strlen(path)] = '\';
tPath[strlen(path) + 1] = '*';
tPath[strlen(path) + 2] = ' ';
//参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
struct _finddata_t data;
long handle = _findfirst(tPath , &data);//-1表示没有文件
int ret = handle;
while(ret >= 0)
{
if (data.attrib != _A_SUBDIR)
{
char t[1000];
strcpy(t, path);
strcat(t, "\");
strcat(t, data.name);
remove(t);
}
ret = _findnext(handle, &data);
}
_findclose(handle);
dropDir(path);
free(tPath);
}
int listDir(char *path, char dirList[][20])
{
char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
strcpy(tPath, path);
tPath[strlen(path)] = '\';
tPath[strlen(path) + 1] = '*';
tPath[strlen(path) + 2] = ' ';
//参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
struct _finddata_t data;
int cnt = 0;
long handle = _findfirst(tPath , &data);//-1表示没有文件
int ret = handle;
while(ret >= 0)
{
if (data.attrib == _A_SUBDIR && data.name[0] != '.')
strcpy(dirList[cnt++], data.name);
ret = _findnext(handle, &data);
}
_findclose(handle);
free(tPath);
return cnt;
}
int createFile(char *path)
{
return creat(path, 7)>=0?0:-1;
}
int dropFile(char *path)
{
return remove(path)>=0?0:-1;
}
int listFile(char *path, char fileList[][20])
{
char *tPath = (char*)malloc(sizeof(char) * (strlen(path) + 3));
strcpy(tPath, path);
tPath[strlen(path)] = '\';
tPath[strlen(path) + 1] = '*';
tPath[strlen(path) + 2] = ' ';
//参考 https://baijiahao.baidu.com/s?id=1634104829954130656&wfr=spider&for=pc
struct _finddata_t data;
int cnt = 0;
long handle = _findfirst(tPath , &data);//-1表示没有文件
int ret = handle;
while(ret >= 0)
{
if (data.attrib != _A_SUBDIR)
strcpy(fileList[cnt++], data.name);
ret = _findnext(handle, &data);
}
_findclose(handle);
free(tPath);
return cnt;
}



