遍历目录内所有子目录及子目录中的文件

2010-03-13 09:28

//遍历目录内所有子目录及子目录中的文件,Flag=0仅列举,Flag=1仅删除文件,Flag=2删除所有文件及所有子目录
/*
     //Daniel QQ9562512
    //调用示例
    sDir = ExtractFilePath(sFileName);
    TStrings *StringList = new TStringList();
    ListAllFile(sDir,2,StringList) ;
*/
void   __fastcall   ListAllFile(String Dir,int Flag,TStrings *List)
{
    TSearchRec   sr;
    int   iAttributes   =   0;
    AnsiString fileName,dirStr;
    iAttributes   |=   faAnyFile;
    if   (   FindFirst(   Dir+"*.*",   iAttributes,   sr)   ==   0   )
    {
        try
        {
            do
            {
                if   (   sr.Name == "." || sr.Name == ".." )
                    continue;
                if   (   sr.Attr == faDirectory )
                {
                    List->Add(Dir+sr.Name+"//");//记录子目录名

                    ListAllFile( Dir+sr.Name+"//",Flag,List );//递归查找并处理子目录中所有文件
                    //此处可以删除文件已经被删除过的空的子目录
                    if(Flag==2)
                    {
                        RemoveDir(Dir+sr.Name+"//");
                    }
                }
                else
                {
                    List->Add(Dir + sr.Name);//记录文件名
                    Application->ProcessMessages();
                    //DeleteFile   (   Dir   +   "//"   +   sr.Name   );//删除文件
                    if(Flag==1 || Flag==2)
                    {
                        DeleteFile(Dir + sr.Name);
                    }
                }
                Application->ProcessMessages();
            }while   (   FindNext   (   sr   )   ==   0   );
            //删除文件夹
            if(Flag==2)
            {
                RemoveDir(Dir);
            }
            Application->ProcessMessages();

            FindClose(sr);
        }
        catch(Exception   &e)  
        {
            FindClose(sr);
        }  
    }
}

 

///

jishiping前辈给我的查找函数,你可以参考改进一下
void   SearchFile(LPCSTR   path,   TStringList*   List)
{
        char   *s;
        char   File[512];
        HANDLE   hFind;
        WIN32_FIND_DATA   fd;
       
        strcpy(File,   path);
        s   =   File   +   strlen(File);
        if   (*(s-1)   !=   '// ')
                *s++   =   '// ';
        strcpy(s,   "*.txt ");//你把这个   *.txt用*.*替换可以搜索全部文件
        hFind   =   FindFirstFile(File,   &fd);
        if   (hFind==INVALID_HANDLE_VALUE)
                return;
        do   {
                strcpy(s,   fd.cFileName);
                if   (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)   {
                        //添加对文件夹的处理
                        if   (*s   !=   '. ')   //忽略   ". "   和   ".. "
                                SearchFile(File);
                }
                else   {   //文件
                        List-> Add(File);
                }
        }while(FindNextFile(hFind,&fd));
        FindClose(hFind);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值