使用方法:将.c和.cpp文件添加到你的项目中,然后#include “FileDirTool.h”
#include <stdio.h>
#include "FileDirTools.h"
int main()
{
return 0;
}
#ifndef FILEDIRTOOLS_H_INCLUDED
#define FILEDIRTOOLS_H_INCLUDED
#include <direct.h>
#include <io.h>
#include <malloc.h>
#include <string.h>
int createDir(char* path);
int dropDir(char* path);
void cascadeDropDir(char* path);
int listDir(char *path, char dirList[][20]);
int createFile(char *path);
int dropFile(char *path);
int listFile(char *path, char fileList[][20]);
#endif
#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] = '\0';
struct _finddata_t data;
long handle = _findfirst(tPath , &data);
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] = '\0';
struct _finddata_t data;
int cnt = 0;
long handle = _findfirst(tPath , &data);
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] = '\0';
struct _finddata_t data;
int cnt = 0;
long handle = _findfirst(tPath , &data);
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;
}
|