Recursives löschen von Verzeichnissen mittels QT



  • HeaderFile (advqdir.h):

    /***************************************************************************
                              advqdir.h  -  description
                                 -------------------
        begin                : Sun Oct 27 2002
        copyright            : (C) 2002 by Michael Buesch
        email                : freesoftwaredeveloper@web.de
     ***************************************************************************/
    
    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/
    
    #ifndef ADVQDIR_H
    #define ADVQDIR_H
    
    #include <qdir.h>
    
    using namespace std;
    
    /**AdvancedQDir. Implements advanced member-functions to QDir. (deleting directories recursively)
      *@author Michael Buesch
      */
    
    class AdvQDir : public QDir  {
    public:
      AdvQDir();
      AdvQDir( const QString &path, const QString &nameFilter = QString::null,
              int sortSpec = Name | IgnoreCase, int filterSpec = All );
      AdvQDir( const QDir &d );
      ~AdvQDir();
      /** delete a directory recursively (including subdirectories an files) */
      virtual bool recRmdir( const QString &dirName ) const;
    };
    
    #endif
    

    CodeFile (advqdir.cpp):

    /***************************************************************************
                              advqdir.cpp  -  description
                                 -------------------
        begin                : Sun Oct 27 2002
        copyright            : (C) 2002 by Michael Buesch
        email                : freesoftwaredeveloper@web.de
     ***************************************************************************/
    
    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/
    
    #include <qfileinfo.h>
    #include <qfile.h>
    #include <qstringlist.h>
    
    #include "advqdir.h"
    
    AdvQDir::AdvQDir() : QDir()
    {
    }
    
    AdvQDir::AdvQDir( const QString &path, const QString &nameFilter, int sortSpec, int filterSpec )
      : QDir(path, nameFilter, sortSpec, filterSpec)
    {
    }
    
    AdvQDir::AdvQDir( const QDir &d ) : QDir(d)
    {
    }
    
    AdvQDir::~AdvQDir()
    {
    }
    
    bool AdvQDir::recRmdir( const QString &dirName ) const
    {
      QString dirN = dirName;
      if(isRelativePath(dirN)) // if not absolute path, make it absolute
        dirN = current().path() + dirN; // FIXME: I'm not really sure that this works
      QDir dir(dirN);
      QStringList list = dir.entryList(QDir::All); // make list of entries in directory
      unsigned int i, lstCnt = list.count();
      QFileInfo fileInfo;
      QString curItem, lstAt;
      for(i = 0; i < lstCnt; i++){ // loop through all items of list
        lstAt = *list.at(i);
        if(!(lstAt == ".") && !(lstAt == "..")){
          curItem = dirN + "/" + lstAt;
          fileInfo.setFile(curItem);
          if(fileInfo.isDir())      // is directory
            recRmdir(curItem);      // call recRmdir() recursively for deleting subdirectory
          else                      // is file
            QFile::remove(curItem); // ok, delete file
        }
      }
      dir.cdUp();
      return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull
    }
    

    [edit]Thread auf das wichtige gekürzt und neue performante Version eingefügt[/edit]


Anmelden zum Antworten