How to delete a folder in C++?

后端 未结 16 1285
悲&欢浪女
悲&欢浪女 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:04

    Use SHFileOperation to remove the folder recursivelly

    0 讨论(0)
  • 2020-12-03 01:05

    I strongly advise to use Boost.FileSystem.

    http://www.boost.org/doc/libs/1_38_0/libs/filesystem/doc/index.htm

    In your case that would be

    boost::filesystem::remove_all(yourPath)

    0 讨论(0)
  • 2020-12-03 01:05

    The directory must be empty and your program must have permissions to delete it

    but the function called rmdir will do it

    rmdir("C:/Documents and Settings/user/Desktop/itsme") 
    
    0 讨论(0)
  • 2020-12-03 01:05

    The C++ Standard defines the remove() function, which may or may not delete a folder, depending on implementation. If it doesn't you need to use an implementation specific function such as rmdir().

    0 讨论(0)
  • 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 <string>
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <windows.h>
    #include <conio.h>
    
    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;
    }
    
    0 讨论(0)
  • 2020-12-03 01:10

    Try use system "rmdir -s -q file_to_delte".
    This will delete the folder and all files in it.

    0 讨论(0)
提交回复
热议问题