PDA

View Full Version : QFileInfo Creation



QbelcorT
17th June 2009, 06:46
Hi,
I know QFileInfo can get the creation date of a file. Is there an effiecient method in for finding the oldest file in a directory using Qt functions?. The directory will have a maximum of 125 files in it. I know in linux I can use this ls -lat | grep ^- | tail -1 | head -1. but I prefer not using this method.
I need the speed to be fast. Any suggestions?
Thank you.

spirit
17th June 2009, 07:04
I think this QDir::entryInfoList should help you.

spirit
17th June 2009, 07:19
an example


...
QDir dir("d:/");
QStringList entities = dir.entryList(QDir::Files, QDir::Time);
qDebug() << entities;
...

a last file is oldest.

shentian
17th June 2009, 09:01
Not quite, QDir::Time sorts by modification date, not creation date. You will have to read all QFileInfo's and do the comparison yourself:


QDir dir("d:/");
QFileInfo oldest;
foreach (QFileInfo fileInfo, dir.entryInfoList())
{
if (oldest.filePath().isEmpty() || fileInfo.created() < oldest.created())
{
oldest = fileInfo;
}
}
qDebug() << "Oldest file is: " << oldest.filePath();