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:
{
fileListing <<
QFileInfo( dir,
file).
absoluteFilePath();
foreach
(QString subDir, dir.
entryList( QDir::Dirs |
QDir::NoDotAndDotDot)) fileListing << getFiles
( path
+ QDir::separator() + subDir
);
return fileListing;
}
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;
}
To copy to clipboard, switch view to plain text mode
I then deleted the files using:
int count = fileListing.size();
for( i=0; i<count; i++)
QFile::remove( fileListing
[i
]);
int count = fileListing.size();
for( i=0; i<count; i++)
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:
{
dirListing <<
QFileInfo( dir, dirs
).
absoluteFilePath();
foreach
(QString subDir, dir.
entryList( QDir::Dirs |
QDir::NoDotAndDotDot)) dirListing << getDirs
( path
+ QDir::separator() + subDir
);
return dirListing;
}
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;
}
To copy to clipboard, switch view to plain text mode
And attempted to delete them using:
int cnt = dirListing.size() - 1;
for( i = cnt; i = 0; i--)
QDir::rmdir( dirListing
[i
]);
int cnt = dirListing.size() - 1;
for( i = cnt; i = 0; i--)
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:
int cnt = dirListing.size() - 1;
for( i = cnt; i = 0; i--)
dir.rmdir( dirListing[i]);
int cnt = dirListing.size() - 1;
for( i = cnt; i = 0; i--)
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.
Bookmarks