Results 1 to 20 of 20

Thread: How to remove empty folders?

  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

  13. #12
    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?

    can you show path and dirs structure?

  14. #13
    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?

    The Path is "/private/var/root/Desktop/Root"
    "/private/var/root/Desktop/Root/GIF"
    Always Believe in Urself
    Merry

  15. #14
    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?

    looks like correct dir path.
    maybe dir has a rea-only files or it's read-only. did you check it?

  16. #15
    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 I check, the files are not read only, Files and dirs both are Read & Write.
    Always Believe in Urself
    Merry

  17. #16
    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?

    I've just tested code from my post under Linux, it works fine.
    try to put qDebug() and output result of dir and files removing.

  18. #17
    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, When i run the code under Windows then it removes all the files and folders, and in MAC it remove all the files and also folders that are under root dir except the root dir,dont understand wat to do?
    Always Believe in Urself
    Merry

  19. #18
    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?

    yup, under Linux the same situation, directory "/private" can't be removed, because you have no rights to do this, but I try under "root" to delete and all content and "/private" directory was removed successfully.

  20. #19
    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?

    no, I am login as a root only..
    Always Believe in Urself
    Merry

  21. #20
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to remove empty folders?

    That recursive code worked for me,, thanxx a lot @ spirit.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.