PDA

View Full Version : Deleting a Folder



GTBuilder
7th June 2008, 18:35
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:



QStringList Clone::getFiles( const QString &path)
{
QDir dir( path);
QStringList fileListing;
foreach ( QString file, dir.entryList( QDir::Files))
fileListing << QFileInfo( dir, file).absoluteFilePath();

foreach (QString subDir, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
fileListing << getFiles( path + QDir::separator() + subDir);

return fileListing;
}


I then deleted the files using:



int count = fileListing.size();
for( i=0; i<count; i++)
QFile::remove( fileListing[i]);


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



QStringList Clone::getDirs( const QString &path)
{
QDir dir( path);
QStringList dirListing;
foreach ( QString dirs, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
dirListing << QFileInfo( dir, dirs).absoluteFilePath();

foreach (QString subDir, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot))
dirListing << getDirs( path + QDir::separator() + subDir);

return dirListing;
}


And attempted to delete them using:



int cnt = dirListing.size() - 1;

for( i = cnt; i = 0; i--)
QDir::rmdir( dirListing[i]);


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



int cnt = dirListing.size() - 1;

for( i = cnt; i = 0; i--)
dir.rmdir( dirListing[i]);


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

JimDaniel
7th June 2008, 18:39
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...



void VideoManagerPage::deleteVideoDirectory(QString video_dir)
{
QDir dvd_dir(video_dir);

//First delete any files in the current directory
QFileInfoList files = dvd_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
for(int file = 0; file < files.count(); file++)
{
dvd_dir.remove(files.at(file).fileName());
}

//Now recursively delete any child directories
QFileInfoList dirs = dvd_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs);
for(int dir = 0; dir < dirs.count(); dir++)
{
this->deleteVideoDirectory(dirs.at(dir).absoluteFilePath ());
}

//Finally, remove empty parent directory
dvd_dir.rmdir(dvd_dir.path());
}

JimDaniel
8th June 2008, 02:44
for( i = cnt; i = 0; i--)
QDir::rmdir( dirListing[i]);


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:



for(i = cnt; i >= 0; i--)
{
QDir dir(dirListing[i]);
dir.cdUp();
dir.rmdir(dirListing[i]);
}

GTBuilder
8th June 2008, 03:20
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.

JimDaniel
9th June 2008, 06:14
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.

GTBuilder
9th June 2008, 13:15
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.

lyuts
11th June 2008, 10:12
The problem is that your trying to delete you dir like this


QDir::rmdir( dirListing[i]);


But rmdir is not static. What you should do is create QDir object (let say QDir dir) and then


dir.rmdir(...);

skimber
19th August 2008, 15:47
Why can't you use QProcess to remove the folder? This destroys the folder and everything in it.


QProcess delfiles;
delfiles.start("rm -R /folder/files");

rbp
29th September 2009, 02:32
well that's not going to work cross platform

spirit
29th September 2009, 06:22
read this (http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-remove-empty-folders-15224.html/?highlight=rmdir) thread, maybe it should help you.