简体   繁体   中英

How to remove directories using c++ in windows

I'm trying to remove directories using this code, but this wouldn't work. And, I couldn't found the issue. Maybe lack of privileges on the deletion operation on Windows?

Here is the code that I'm using:

#define _CRT_SECURE_NO_DEPRECATE
#include <string>
#include <iostream>

#include <windows.h>
#include <conio.h>


int DeleteDirectory(const std::string &refcstrRootDirectory,
    bool              bDeleteSubdirectories = true)
{
    bool            bSubdirectory = false;       // Flag, indicating whether
    // subdirectories have been found
    HANDLE          hFile;                       // Handle to directory
    std::string     strFilePath;                 // Filepath
    std::string     strPattern;                  // Pattern
    WIN32_FIND_DATA FileInformation;             // File information


    strPattern = refcstrRootDirectory + "\\*.*";
    hFile = ::FindFirstFile((LPCWSTR)strPattern.c_str(), &FileInformation);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (FileInformation.cFileName[0] != '.')
            {
                strFilePath.erase();
                strFilePath = refcstrRootDirectory + "\\" + (char*)FileInformation.cFileName;

                if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                {
                    if (bDeleteSubdirectories)
                    {
                        // Delete subdirectory
                        int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories);
                        if (iRC)
                            return iRC;
                    }
                    else
                        bSubdirectory = true;
                }
                else
                {
                    // Set file attributes
                    if (::SetFileAttributes((LPCWSTR)strFilePath.c_str(),
                        FILE_ATTRIBUTE_NORMAL) == FALSE)
                        return ::GetLastError();

                    // Delete file
                    if (::DeleteFile((LPCTSTR)strFilePath.c_str()) == FALSE)
                        return ::GetLastError();
                }
            }
        } while (::FindNextFile(hFile, &FileInformation) == TRUE);

        // Close handle
        ::FindClose(hFile);

        DWORD dwError = ::GetLastError();
        if (dwError != ERROR_NO_MORE_FILES)
            return dwError;
        else
        {
            if (!bSubdirectory)
            {
                // Set directory attributes
                if (::SetFileAttributes((LPCWSTR)refcstrRootDirectory.c_str(),
                    FILE_ATTRIBUTE_NORMAL) == FALSE)
                    return ::GetLastError();

                // Delete directory
                if (::RemoveDirectory((LPCWSTR)refcstrRootDirectory.c_str()) == FALSE)
                    return ::GetLastError();
            }
        }
    }

    return 0;
}


int main()
{
    int         iRC = 0;
    std::string strDirectoryToDelete = "C:\\Users\\AbysCo\\Desktop\\del";


    // Delete 'c:\mydir' without deleting the subdirectories
    iRC = DeleteDirectory(strDirectoryToDelete, false);
    if (iRC)
    {
        std::cout << "Error " << iRC << std::endl;
        return -1;
    }

    // Delete 'c:\mydir' and its subdirectories
    iRC = DeleteDirectory(strDirectoryToDelete);
    if (iRC)
    {
        std::cout << "Error " << iRC << std::endl;
        return -1;
    }

    // Wait for keystroke
    _getch();

    return 0;
}

OS: Windows 7 SP1.

Any brilliant idea, please?

The Windows API already comes with a function to delete entire directories. That is SHFileOperation . And on Vista or later you use IFileOperation for the same purpose.

Not only does this make the process trivial, not much more than a one liner, but it has other benefits:

  1. You can put the deleted directory into the recycle bin if you choose.
  2. You can show the standard system progress dialog if you choose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM