清空多级目录,C++,Win32

本文介绍了一个C++程序示例,展示了如何通过递归方式删除指定目录及其所有子目录和文件。利用Windows API函数FindFirstFile, FindNextFile 和 RemoveDirectory实现文件夹清理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// testTmpMfc.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"
#include 
"testTmpMfc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include 
<string>
#include 
<iostream>
#include 
<afxinet.h>

#define _WIN32_WINNT 0x0400
#include 
<windows.h>


// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

bool rmdirHelper( std::string dir );

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

    WIN32_FIND_DATA FindFileData;            
//查找文件时要使用的数据结构
    HANDLE hFind = INVALID_HANDLE_VALUE;    //定义查找句柄
    DWORD dwError;
    
    
string target = "./a/";
    cout 
<< "Target Dir: " << target << endl;
    
string file ="./a/*";
    
    hFind 
= FindFirstFile( file.c_str(), &FindFileData);//使用FindFirstFile函数来开始文件查找
    
    
if (hFind == INVALID_HANDLE_VALUE){
        CreateDirectory( target.c_str(),NULL );
    }
else{
        cout 
<< "Result: " << rmdirHelper( target ) << endl;
    }


    dwError 
= GetLastError();

    
if (dwError == ERROR_NO_MORE_FILES){
        FindClose(hFind);                            
//关闭查找句柄
    }
else{
        cout 
<< "Find Next File Error: " << dwError << endl;
    }


    
string exit;
    cin 
>> exit;
    
return 0;
}


bool rmdirHelper(std::string dir){

    WIN32_FIND_DATA FindFileData;            
//查找文件时要使用的数据结构
    HANDLE hFind = INVALID_HANDLE_VALUE;    //定义查找句柄
    DWORD dwError;
    
    
if( dir[dir.size()-1!= '/')
        dir.append(
"/");

    
string searchMark = dir;
    searchMark.append(
"*");

    hFind 
= FindFirstFile( searchMark.c_str(), &FindFileData);//使用FindFirstFile函数来开始文件查找
    
    
if (hFind == INVALID_HANDLE_VALUE) 
    
{
        
return false;
    }
 
    
else 
    
{
        
//以下是循环使用FindNextFile函数来查找文件
        do
        
{
            
//如果找到的是目录
            if( ( FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY ) ){
                
//如果是"."或者"..",就跳过
                if( strcmp( FindFileData.cFileName,"." ) == 0 || strcmp(FindFileData.cFileName,".."== 0 ){
                    
continue;
                
//否则就递归调用
                }

                
else
                
{
                    
string nextDir = dir;
                    nextDir.append(
"/");
                    nextDir.append( FindFileData.cFileName );
                    
if( rmdirHelper( nextDir ) == false){
                        cout 
<< "Clear " << nextDir << " fail." << endl;
                    }
else{
                        RemoveDirectory( nextDir.c_str() );
                    }

                }

            
//如果找到的是文件,就删除
            }
else{
                
string delFile = dir;
                delFile.append( FindFileData.cFileName );
                
if( DeleteFile( delFile.c_str() ) == false )
                    cout 
<< "Delete " << delFile << " fail" << endl;
            }

        }
while( FindNextFile( hFind,&FindFileData ) != 0 );

        dwError 
= GetLastError();

        
if (dwError == ERROR_NO_MORE_FILES) 
        
{
            FindClose(hFind);                            
//关闭查找句柄
        }
 
        
else 
        
{
            cout 
<< "Find Next File Error: " << dwError << endl;
            
return false;
        }

    }

    
return true;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值