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.
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.