跨平台实现删除文件夹中所有文件、移动文件的功能

支持 windows、linux

DeleteDirectory:清空文件夹
MoveFile_ :移动文件

windows的

BOOL IsDirectory(const char *pDir)  
{  
    char szCurPath[500];  
    ZeroMemory(szCurPath, 500);  
    sprintf_s(szCurPath, 500, "%s//*", pDir);  
    WIN32_FIND_DATAA FindFileData;        
    ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));  

    HANDLE hFile = FindFirstFile(szCurPath, &FindFileData); /**< find first file by given path. */  

    if( hFile == INVALID_HANDLE_VALUE )   
    {  
        FindClose(hFile);  
        return FALSE; /** 如果不能找到第一个文件,那么没有目录 */  
    }else  
    {     
        FindClose(hFile);  
        return TRUE;  
    }  

} 
BOOL DeleteDirectory(const char * DirName)  
{  
// CFileFind tempFind; //声明一个CFileFind类变量,以用来搜索 
    char szCurPath[MAX_PATH];       //用于定义搜索格式 
    _snprintf(szCurPath, MAX_PATH, "%s//*.*", DirName); //匹配格式为*.*,即该目录下的所有文件 
    WIN32_FIND_DATAA FindFileData;        
    ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATAA));  
    HANDLE hFile = FindFirstFile(szCurPath, &FindFileData);  
    BOOL IsFinded = TRUE;  
    while(IsFinded)  
    {  
        IsFinded = FindNextFile(hFile, &FindFileData); //递归搜索其他的文件 
        if( strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, "..") ) //如果不是"." ".."目录 
        {  
            string strFileName = "";  
            strFileName = strFileName + DirName + "//" + FindFileData.cFileName;  
            string strTemp;  
            strTemp = strFileName;  
            if( IsDirectory(strFileName.c_str()) ) //如果是目录,则递归地调用 
            {     
                printf("目录为:%s/n", strFileName.c_str());  
                DeleteDirectory(strTemp.c_str());  
            }  
            else  
            {  
                DeleteFile(strTemp.c_str());  
            }  
        }  
    }  
    FindClose(hFile);  

    //BOOL bRet = RemoveDirectoryA(DirName); 
    //if( bRet == 0 ) //删除目录 
    //{ 
    // printf("删除%s目录失败!/n", DirName); 
    // return FALSE; 
    //} 
    return TRUE;  
}
void MoveFile_(char *from,char *to)
{
    MoveFile(from,to);
}

linux的

#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>

//判断是否为目录
bool is_dir(const char *path)
{
    struct stat statbuf;
    if(lstat(path, &statbuf) ==0)//lstat返回文件的信息,文件信息存放在stat结构中
    {
        return S_ISDIR(statbuf.st_mode) != 0;//S_ISDIR宏,判断文件类型是否为目录
    }
    return false;
}

//判断是否为常规文件
bool is_file(const char *path)
{
    struct stat statbuf;
    if(lstat(path, &statbuf) ==0)
        return S_ISREG(statbuf.st_mode) != 0;//判断文件是否为常规文件
    return false;
}

//判断是否是特殊目录
bool is_special_dir(const char *path)
{
    return strcmp(path, ".") == 0 || strcmp(path, "..") == 0;
}

//生成完整的文件路径
void get_file_path(const char *path, const char *file_name,  char *file_path)
{
    strcpy(file_path, path);
    if(file_path[strlen(path) - 1] != '/')
        strcat(file_path, "/");
    strcat(file_path, file_name);
}

void DeleteDirectory(const char *path)
{
    DIR *dir;
    dirent *dir_info;
    char file_path[PATH_MAX];
    if(is_file(path))
    {
        remove(path);
        return;
    }
    if(is_dir(path))
    {
        if((dir = opendir(path)) == NULL)
            return;
        while((dir_info = readdir(dir)) != NULL)
        {
            get_file_path(path, dir_info->d_name, file_path);
            if(is_special_dir(dir_info->d_name))
                continue;
            DeleteDirectory(file_path);
            rmdir(file_path);
        }
    }
}
void MoveFile_(char *from,char *to)
{
    std::string cmd = std::string("mv ")+std::string(from)+std::string(" ")+std::string(to);
    system(cmd.c_str());
}

你可能感兴趣的:(跨平台-文件)