Results 1 to 10 of 10

Thread: Deleting a Folder

  1. #1
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleting a Folder

    In my application, I need to delete all the files in a folder as well as all the sub-directories.
    I've looked at several discussions of this using API functions and quite frankly I understood very little of it. Using brute force and ignorance, I arrived at the following:

    Qt Code:
    1. QStringList Clone::getFiles( const QString &path)
    2. {
    3. QDir dir( path);
    4. QStringList fileListing;
    5. foreach ( QString file, dir.entryList( QDir::Files))
    6. fileListing << QFileInfo( dir, file).absoluteFilePath();
    7.  
    8. foreach (QString subDir, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
    9. fileListing << getFiles( path + QDir::separator() + subDir);
    10.  
    11. return fileListing;
    12. }
    To copy to clipboard, switch view to plain text mode 

    I then deleted the files using:

    Qt Code:
    1. int count = fileListing.size();
    2. for( i=0; i<count; i++)
    3. QFile::remove( fileListing[i]);
    To copy to clipboard, switch view to plain text mode 

    Flushed with success, I tackled the directories in the same way:

    Qt Code:
    1. QStringList Clone::getDirs( const QString &path)
    2. {
    3. QDir dir( path);
    4. QStringList dirListing;
    5. foreach ( QString dirs, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
    6. dirListing << QFileInfo( dir, dirs).absoluteFilePath();
    7.  
    8. foreach (QString subDir, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
    9. dirListing << getDirs( path + QDir::separator() + subDir);
    10.  
    11. return dirListing;
    12. }
    To copy to clipboard, switch view to plain text mode 

    And attempted to delete them using:

    Qt Code:
    1. int cnt = dirListing.size() - 1;
    2.  
    3. for( i = cnt; i = 0; i--)
    4. QDir::rmdir( dirListing[i]);
    To copy to clipboard, switch view to plain text mode 

    Got a compiler error for QDir::rmdir( ) need an object. Not clear why QDir acts different from QFile. Assume I need something like:

    Qt Code:
    1. int cnt = dirListing.size() - 1;
    2.  
    3. for( i = cnt; i = 0; i--)
    4. dir.rmdir( dirListing[i]);
    To copy to clipboard, switch view to plain text mode 

    but have no idea how to define dir that will actually delete directories.

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Deleting a Folder

    I don't have time right now to go through your code, but in a past project, I wrote a recursive function to do just what you're wanting to do, you pass it the parent directory path as a string, and it does the rest for you...

    Qt Code:
    1. void VideoManagerPage::deleteVideoDirectory(QString video_dir)
    2. {
    3. QDir dvd_dir(video_dir);
    4.  
    5. //First delete any files in the current directory
    6. QFileInfoList files = dvd_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
    7. for(int file = 0; file < files.count(); file++)
    8. {
    9. dvd_dir.remove(files.at(file).fileName());
    10. }
    11.  
    12. //Now recursively delete any child directories
    13. QFileInfoList dirs = dvd_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs);
    14. for(int dir = 0; dir < dirs.count(); dir++)
    15. {
    16. this->deleteVideoDirectory(dirs.at(dir).absoluteFilePath());
    17. }
    18.  
    19. //Finally, remove empty parent directory
    20. dvd_dir.rmdir(dvd_dir.path());
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 7th June 2008 at 19:46.

  3. The following user says thank you to JimDaniel for this useful post:

    missoni (6th April 2011)

  4. #3
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Deleting a Folder

    Qt Code:
    1. for( i = cnt; i = 0; i--)
    2. QDir::rmdir( dirListing[i]);
    To copy to clipboard, switch view to plain text mode 

    One problem I see here is that you're setting i = 0 in the for loop's conditional test - you want it to read i >= 0. That may be causing your error as after i is set to 0, you then decrement it, so the next go round its trying to access dirListing[-1], besides being stuck in an infinite loop.

    EDIT: After looking through the documentation, I see what you're saying, there is not static function QDir::rmdir(); Well, if you don't want to rethink your algorithm, I found a function called cdUp() in QDir, but I've never used it so I can't speak for it. Seems like this would work though:

    Qt Code:
    1. for(i = cnt; i >= 0; i--)
    2. {
    3. QDir dir(dirListing[i]);
    4. dir.cdUp();
    5. dir.rmdir(dirListing[i]);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JimDaniel; 8th June 2008 at 04:05.

  5. #4
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a Folder

    Jim,

    Thanks very much for your input. Big advantage to your approach is I don't end up with a huge QStringList containing a list of all the files in the directory, (>2000 possible).

    Routine works, but one problem remaining. Directory is from a commercial application and can contain files with system attribute or others that prevent removal, thus hanging the complete deletion of the directory.

    I need a QFile::setPermission( ?) I guess.

  6. #5
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Deleting a Folder

    Maybe, I'm not too familiar with setPermissions(). If you are accessing files not created by the program you may run into trouble randomly, unless you have some kind of control over the files. Glad my algorithm can be of help to you.

  7. #6
    Join Date
    Jan 2008
    Posts
    31
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a Folder

    JIm,

    setPermissions doesn't work. One of the problems is that some files have 'hidden' attribute so the QFileInfoList doesn't even see them. I had to use QProcess to launch an MSDOS batch file:

    CD %1
    ATTRIB -h -s -r /s
    EXIT

    and add a delay before launching the delete files algorithm.

    I'm sure there's a much cleaner way using WIN32 API functions because an entire folder can be sent to the Recycle Bin with a single key-stroke.

  8. #7
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting a Folder

    The problem is that your trying to delete you dir like this
    Qt Code:
    1. QDir::rmdir( dirListing[i]);
    To copy to clipboard, switch view to plain text mode 

    But rmdir is not static. What you should do is create QDir object (let say QDir dir) and then
    Qt Code:
    1. dir.rmdir(...);
    To copy to clipboard, switch view to plain text mode 
    I'm a rebel in the S.D.G.

  9. #8
    Join Date
    Jul 2008
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Deleting a Folder

    Why can't you use QProcess to remove the folder? This destroys the folder and everything in it.

    Qt Code:
    1. QProcess delfiles;
    2. delfiles.start("rm -R /folder/files");
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting a Folder

    well that's not going to work cross platform

  11. #10
    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: Deleting a Folder

    read this thread, maybe it should help you.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Avoiding unnecessary files and folder in Impelementation
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2008, 15:54
  2. QFtp: entering deep folder (cd)
    By Raccoon29 in forum Newbie
    Replies: 1
    Last Post: 13th May 2008, 10:28
  3. Deleting QProcess
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 29th January 2008, 19:55
  4. Replies: 2
    Last Post: 27th October 2007, 19:16
  5. [QT3] Temporary Folder Temporary File
    By Senpai in forum Qt Programming
    Replies: 10
    Last Post: 6th October 2006, 11:54

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.