Public Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long

解决方案 »

  1.   

    FileFinder.h
    ============
    // FileFinder.h: interface for the CFileFinder class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_FILEFINDER_H__E48F52E6_34DA_11D4_A56C_5254AB136A75__INCLUDED_)
    #define AFX_FILEFINDER_H__E48F52E6_34DA_11D4_A56C_5254AB136A75__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000typedef BOOL (FILEFINDER_CALLBACK_FUNCTION)(LPCTSTR lpszBase, LPWIN32_FIND_DATA lpFindFileData);class CFileFinder  
    {
    public:
    BOOL Start(LPCTSTR lpszBase, LPCTSTR lpszPattern, FILEFINDER_CALLBACK_FUNCTION *pfnCallBack);
    CFileFinder();
    virtual ~CFileFinder();};#endif // !defined(AFX_FILEFINDER_H__E48F52E6_34DA_11D4_A56C_5254AB136A75__INCLUDED_)FileFinder.cpp
    ==============
    // FileFinder.cpp: implementation of the CFileFinder class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "FileFinder.h"//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////BOOL CreatePattern(LPCTSTR lpszBase, LPCTSTR lpszPattern, LPTSTR lpszBuffer, DWORD cchBufLen, LPDWORD lpcchBuffer);CFileFinder::CFileFinder()
    {}CFileFinder::~CFileFinder()
    {}BOOL CFileFinder::Start(LPCTSTR lpszBase, LPCTSTR lpszPattern, FILEFINDER_CALLBACK_FUNCTION *pfnCallBack)
    {
    WIN32_FIND_DATA structFindFileData;
    HANDLE hFileFind;
    TCHAR szBuffer[MAX_PATH];
    DWORD cchNeeded;
    int bHasError; if(!CreatePattern(lpszBase, lpszPattern, szBuffer, MAX_PATH, &cchNeeded))
    return FALSE;
    //MessageBox(NULL, szBuffer, _T("CFileFinder"), MB_OK);
    hFileFind = FindFirstFile(szBuffer, &structFindFileData);
    if(hFileFind == INVALID_HANDLE_VALUE)
    return FALSE;
    bHasError = 0;
    do
    {
    if(!pfnCallBack(lpszBase, &structFindFileData))
    bHasError++;
    }while(FindNextFile(hFileFind, &structFindFileData));
    FindClose(hFileFind); return bHasError == 0;
    }相关函数:
    ========
    BOOL DeleteFolderFilesAndSubDirs(LPCTSTR lpszFolder)
    {
    CFileFinder fileFinder; return fileFinder.Start(lpszFolder, _T("*.*"), DeleteFolderFilesAndSubDirsCallBack);
    }BOOL DeleteFolderFilesAndSubDirsCallBack
    (
    LPCTSTR lpszBase, LPWIN32_FIND_DATA lpFindFileData
    )
    {
    int bHasError;
    DWORD cchNeeded;
    TCHAR szPathName[MAX_PATH]; if(IsDots(lpFindFileData->cFileName))
    return TRUE; bHasError = 0;
    if(!PathCat(lpszBase, lpFindFileData->cFileName, szPathName, MAX_PATH, &cchNeeded))
    return FALSE;
    logFile.WriteLine(szPathName);
    if(lpFindFileData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    if(!DeleteFolderFilesAndSubDirs(szPathName))
    {
    logFile.WriteText(_T("Cannot delete folder files and subdirs of "));
    logFile.WriteLine(szPathName);
    bHasError++;
    }
    if(!RemoveDirectory(szPathName))
    {
    logFile.WriteText(_T("Cannot delete folder "));
    logFile.WriteLine(szPathName);
    bHasError++;
    ::MessageBeep((UINT)-1);
    }
    }
    else
    {
    if(!RemoveFile(szPathName))
    {
    bHasError++;
    logFile.WriteText(_T("Cannot delete file "));
    logFile.WriteLine(szPathName);
    ::MessageBeep((UINT)-1);
    }
    }
    return bHasError == 0;
    }BOOL CreatePattern(LPCTSTR lpszBase, LPCTSTR lpszPattern, LPTSTR lpszBuffer, DWORD cchBufLen, LPDWORD pcchNeeded)
    {
    return PathCat(lpszBase, lpszPattern, lpszBuffer, cchBufLen, pcchNeeded);
    }BOOL IsDots(LPCTSTR lpszFileName)
    {
    if(lpszFileName[0] == _T('.'))
    return TRUE;
    if(_tcscmp(lpszFileName, _T("..")) == 0)
    return TRUE;
    return FALSE;
    }BOOL PathCat(LPCTSTR lpszBase, LPCTSTR lpszSub, LPTSTR lpszBuffer, DWORD cchBufLen, LPDWORD pcchNeeded)
    {
    size_t cchBase;
    size_t cchSub;
    size_t cchNeeded;
    TCHAR tchar; cchBase = _tcslen(lpszBase);
    cchSub = _tcslen(lpszSub);
    cchNeeded = cchBase + cchSub + 1;
    if(cchBufLen < cchNeeded)
    {
    *pcchNeeded = cchNeeded;
    lpszBuffer[0] = _T('\0');
    return FALSE;
    }
    _tcscpy(lpszBuffer, lpszBase);
    tchar = lpszBuffer[cchBase - 1];
    if(tchar != _T('\\') && tchar != _T('/'))
    {
    lpszBuffer[cchBase] = _T('\\');
    lpszBuffer[cchBase + 1] = 0;
    }
    _tcscat(lpszBuffer, lpszSub);
    return TRUE;
    }BOOL RemoveFile(LPCTSTR lpszPathName)
    {
    ::SetFileAttributes(lpszPathName, FILE_ATTRIBUTE_NORMAL);
    return ::DeleteFile(lpszPathName);
    }
      

  2.   

    用WinExec,然后用deletetree xxx/y