Ordner Loeschen



  • Hi,
    ich habe ein Prog was einen Ordner loescht, jetzt moechte ich aber das er nicht den Ordner selbst loescht sondern nur die Dateien die in ihm sind.

    Hoffe ihr koennt mir helfen.

    #include <string>
    #include <iostream>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    int DeleteDirectory(const std::string &refcstrRootDirectory,
                        bool              bDeleteSubdirectories = true)
    {
      bool            bSubdirectory = false;       // Flag, indicating whether
                                                   // subdirectories have been found
      HANDLE          hFile;                       // Handle to directory
      string     strFilePath;                 // Filepath
      string     strPattern;                  // Pattern
      WIN32_FIND_DATA FileInformation;             // File information
    
      strPattern = refcstrRootDirectory + "\\*.*";
      hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
      if(hFile != INVALID_HANDLE_VALUE)
      {
        do
        {
          if(FileInformation.cFileName[0] != '.')
          {
            strFilePath.erase();
            strFilePath = refcstrRootDirectory + "\\" + 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(strFilePath.c_str(),
                                     FILE_ATTRIBUTE_NORMAL) == FALSE)
                return ::GetLastError();
    
              // Delete file
              if(::DeleteFile(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(refcstrRootDirectory.c_str(),
                                   FILE_ATTRIBUTE_NORMAL) == FALSE)
              return ::GetLastError();
    
            // Delete directory
            if(::RemoveDirectory(refcstrRootDirectory.c_str()) == FALSE)
              return ::GetLastError();
          }
        }
      }
    
      return 0;
    }
    
    int main()
    {
      int iRC = 0;
      string strDirectoryToDelete = "c:\\Dokumente und Einstellungen\\Coda\\Desktop\\Matrix_D\\";
    
      // Delete 'c:\mydir' without deleting the subdirectories
      iRC = DeleteDirectory(strDirectoryToDelete, false);
      if(iRC)
      {
        return -1;
      }
    
      // Delete 'c:\mydir' and its subdirectories
      iRC = DeleteDirectory(strDirectoryToDelete);
      if(iRC)
      {
        return -1;
      }
    
      return 0;
    }
    

    MfG
    Coda



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Coda@linux.PrayLoad schrieb:

    [...] jetzt moechte ich aber das er nicht den Ordner selbst loescht sondern nur die Dateien die in ihm sind.

    Dann musst Du diese Dateien wieder enumerieren (FindFirstFile, etc.) und anschließend auf jede einzelne DeleteFile anwenden.



  • Wenn die Unterordnerstruktur erhalten werden soll, dann darf

    if(!bSubDirectory)
    

    nicht wahr werden.



  • amsonsten - ordner loeschen und dann neu erstellen lassen #gg


Anmelden zum Antworten