c/c++常用代码--清空目录

#pragma once

#include <io.h>
#include <stdio.h>
#include <string>
#include <direct.h>


static void clean_dir(const char* szDir)
{
    if (szDir == NULL || strlen(szDir) == 0)
        return;

    std::string strDir = szDir;

    char c = strDir[strDir.length() - 1];
    if (c != '/' && c != '\\')
        strDir += '/';

    struct _finddata_t fd;
    long h=_findfirst((strDir + "*.*").c_str(), &fd);
    if (h == -1)
    {
        return ;
    }

    do
    {
        if (strcmp(fd.name, ".") == 0
            || strcmp(fd.name, "..") == 0)
            continue;

        if (fd.attrib & (_A_SYSTEM | _A_HIDDEN))
            continue;

        if (fd.attrib & _A_SUBDIR)
        {
            std::string str = strDir + fd.name + "/";
            clean_dir(str.c_str());
            rmdir(str.c_str());
            continue;
        }
        
        std::string str = strDir + fd.name;
        printf("%s\n", str.c_str());        

        remove(str.c_str());
    }
    while (_findnext(h, &fd)==0);

    _findclose(h);
}

 

posted @ 2014-08-22 17:32  崇山峻岭  阅读(300)  评论(0编辑  收藏  举报