Results 1 to 20 of 20

Thread: How to remove empty folders?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default How to remove empty folders?

    Hi all

    Working on Qt4.2 on my mac intel machine.


    Qt Code:
    1. void MainWindow::ClearDir(QString dirRoot)
    2. {
    3. ClearAll(dirRoot);
    4. QDir d(dirRoot);
    5. d.cdUp();
    6. d.rmdir(dirRoot);
    7. }
    8.  
    9. /*Function for erasing Folder*/
    10.  
    11. void MainWindow:: ClearAll(QString dirViewPath)
    12. {
    13. QDir dirPath(dirViewPath);
    14. QList<QFileInfo> infLst = dirPath.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
    15.  
    16. foreach(QFileInfo finfo, infLst )
    17. {
    18. if(finfo.isDir() )
    19. {
    20. ClearAll(finfo.absoluteFilePath());
    21. dirPath.rmdir(finfo.absoluteFilePath());
    22. }
    23. else
    24. { //file
    25. QString fileName = dirViewPath + "/" + finfo.fileName();
    26. dirPath.remove(finfo.absoluteFilePath());
    27. }
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    Using this code, I try to remove the existing dir selected using QFileDialog, I will be able to remove all the files but empty folders are left.
    Always Believe in Urself
    Merry

  2. The following user says thank you to merry for this useful post:

    yogeshgokul (16th December 2008)

  3. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    are you really sure that the directory which you try to remove is empty?
    bool QDir::rmdir ( const QString & dirName ) const
    Removes the directory specified by dirName.
    The directory must be empty for rmdir() to succeed.
    Returns true if successful; otherwise returns false.
    See also mkdir().

  4. #3
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to remove empty folders?

    Thanks for the replying.

    When i am trying remove directories, it returns false...

    What it mean, that the directories are not empty..

    For eg. i am having Folder F0 which contains sub folder F1 and F2 Which further contains some files f1,f2,f3,f4..

    using the above code, written in the previous mail, i was able to remove all the files f1,f2,f3,f4 but folders F0, F1 and F2 are still there, is am doing something wrong or is there any other alternative...
    Always Believe in Urself
    Merry

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    try this code
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    2. {
    3. m_pb = new QPushButton(tr("Clear"));
    4. connect(m_pb, SIGNAL(clicked()), SLOT(clearFolder()));
    5. setCentralWidget(m_pb);
    6. }
    7.  
    8. void MainWindow::clearFolder()
    9. {
    10. clearFolder("E:/f0");
    11. }
    12.  
    13. void MainWindow::clearFolder(const QString &folder)
    14. {
    15. QDir dir(folder);
    16. QStringList entries = dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    17. foreach (QString entry, entries) {
    18. QFileInfo fileInfo(folder + "/" + entry);
    19. if (fileInfo.isFile() && !QFile::remove(fileInfo.filePath())) {
    20. qWarning() << "can't remove '" << fileInfo.filePath() << "'";
    21. return;
    22. } else if (fileInfo.isDir())
    23. clearFolder(fileInfo.filePath());
    24. }
    25. if (!dir.rmdir(dir.absolutePath()))
    26. qWarning() << "can't remove '" << dir.absolutePath() << "'";
    27. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to remove empty folders?

    I tried above code , but it also wont able to remove dirs.
    Always Believe in Urself
    Merry

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    very strange, I have run this code and it worked.

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    maybe you don't have permissions to remove dirs?

  9. #8
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to remove empty folders?

    Actually, When i create a new blank folder than it is able to remove ,If i open an existing directory which contains subdirs also using QFileDialog , then it wont able to remove..
    Always Believe in Urself
    Merry

  10. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    maybe dir path is absolute?

  11. #10
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to remove empty folders?

    Yaa the dir path is absolute.
    Always Believe in Urself
    Merry

  12. #11
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to remove empty folders?

    If the path is absolute , then how can i remove direcctories..
    Always Believe in Urself
    Merry

Similar Threads

  1. remove node in xml file
    By mattia in forum Newbie
    Replies: 1
    Last Post: 6th March 2008, 13:25
  2. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07:30
  3. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.