How to delete a folder in C++?

后端 未结 16 1339
悲&欢浪女
悲&欢浪女 2020-12-03 00:45

How can I delete a folder using C++?

If no cross-platform way exists, then how to do it for the most-popular OSes - Windows, Linux, Mac, iOS, Android? Would a POSIX

16条回答
  •  死守一世寂寞
    2020-12-03 01:06

    My own implementation based off hB0 that also allows you to view the number of files in each folder also with a little performance boost.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    union seperated {
      struct {
        unsigned int low;
        unsigned int high;
      } uint;
      unsigned long long ull;
    };
    
    unsigned long long num_dirs  = 1;
    unsigned long long num_files = 0;
    seperated size_files;
    
    int DeleteDirectory( char* refRootDirectory );      //predeclare it
    
    int DeleteDirectory( char* refRootDirectory ) {
        HANDLE      hFile;              // Handle to directory
        std::string strFilePath;            // Filepath
        WIN32_FIND_DATA FileInformation;    // File information
        int     dwError;            // Folder deleting error
        std::string strPattern;         // Pattern
    
        strPattern = (std::string)(refRootDirectory) + "\\*.*";
        hFile = ::FindFirstFile( strPattern.c_str(), &FileInformation );
    
        if( hFile != INVALID_HANDLE_VALUE )
        {
            do {
                if( FileInformation.cFileName[0] != '.' ) {
                    strFilePath.erase();
                    strFilePath = std::string(refRootDirectory) + "\\" + FileInformation.cFileName;
    
                    if( FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
                        DeleteDirectory( (char*)strFilePath.c_str() );
    
                        dwError = ::GetLastError();
                        if( dwError != ERROR_NO_MORE_FILES ) {
                            std::cout << "!ERROR!: [[" << strFilePath.c_str() << "]]\n";
                            return dwError;
                        } else {
                            // Set directory attributes
                            if( ! ::SetFileAttributes(refRootDirectory,FILE_ATTRIBUTE_NORMAL) ) {
                                std::cout << "!ERROR!: [[" << strFilePath.c_str() << "]]\n";
                                return ::GetLastError();
                            }
    
                            // Delete directory
                            if( ! ::RemoveDirectory(refRootDirectory) ) {
                                std::cout << "!ERROR!: [[" << strFilePath.c_str() << "]]\n";
                                return ::GetLastError();
                            }
                        }
    
                        ++num_dirs;
                    } else {
    
                        // Set file attributes
                        if( ! ::SetFileAttributes(strFilePath.c_str(),FILE_ATTRIBUTE_NORMAL) ) {
                            std::cout << "!ERROR!: [[" << strFilePath.c_str() << "]]\n";
                            return ::GetLastError();
                        }
    
                        // Delete file
                        if ( ! ::DeleteFile(strFilePath.c_str()) ) {
                            std::cout << "!ERROR!: [[" << strFilePath.c_str() << "]]\n";
                            return ::GetLastError();
                        }
    
                        size_files.ull       += FileInformation.nFileSizeLow;
                        size_files.uint.high += FileInformation.nFileSizeHigh;
    
                        ++num_files;
                    }
                }
            } while( ::FindNextFile(hFile,&FileInformation) );
    
            // Close handle
            ::FindClose( hFile  );
        }
    
        return 0;
    }
    
    unsigned long long num_files_total=0;
    unsigned long long num_dirs_total=0;
    unsigned long long total_size_files=0;
    
    void my_del_directory( char* dir_name ) {
        int iRC = DeleteDirectory( dir_name );
        //int iRC=0;
    
        std::cout << "\"" << dir_name << "\""
                 "\n    Folders: " << num_dirs
              << "\n    Files:   " << num_files
              << "\n    Size:    " << size_files.ull << " Bytes";
        if(iRC)
        {
            std::cout << "\n!ERROR!: " << iRC;
        }
        std::cout << "\n\n";
    
        num_dirs_total   += num_dirs;
        num_files_total  += num_files;
        total_size_files += size_files.ull;
        num_dirs  = 1;
        num_files = 0;
        size_files.ull = 0ULL;
        return;
    }
    
    int main( void )
    {
        size_files.ull = 0ULL;
    
        my_del_directory( (char*)"C:\Windows\temp"      );
            // This will clear out the System temporary directory on windows systems
    
        std::cout << "\n\nResults" << "\nTotal Folders: " << num_dirs_total
                       << "\nTotal Files:   " << num_files_total
                       << "\nTotal Size:    " << total_size_files << " Bytes\n";
    
        return 0;
    }
    

提交回复
热议问题