[Qt-interest] qdiriterator
Soumen Banerjee
soumen08 at gmail.com
Fri Apr 15 03:24:18 CEST 2011
Hi,
You can name the files in a sequence when you generate them. In that case,
it will be a lot simpler to find the newest file then. Just check the names
sequentially and check if they exist with QFileInfo::isFile() .
Another possibility is to use QDirIterator, and just set the first file to
the newest. Make a QFileInfo called newest, and keep updating newest is you
find a file which is newer (QDateTime objects have > operators). This way,
at the end of the loop, youll have newest pointing to the newest file in the
directory.
This is the same thing as a linear pass over an array to find the maximum.
Regards,
Soumen
On Fri, Apr 15, 2011 at 1:46 AM, caius ligarius <caius9090 at gmail.com> wrote:
> Thanks. Does this automatically sort by time or will I need to parse
> through the iterator and pick the last file?
>
> caius
>
>
> On Thu, Apr 14, 2011 at 12:30 PM, Rohan Prabhu <rohan at rohanprabhu.com>wrote:
>
>> Use:
>>
>> QDirIterator dirIter("/home/user/path/to/directory",
>> QStringList(QString("stats*.txt")), QDir::Files);
>>
>> or, if you intend to read those files as well, and hence would like to
>> iterator over files which are readable:
>>
>> QDirIterator dirIter("/home/user/path/to/directory",
>> QStringList(QString("stats*.txt")), QDir::Files|QDir::Readable);
>>
>> then simply iterator over the QDirIterator:
>>
>> while(dirIter.hasNext()) {
>> QString filePath = dirIter.next();
>> // Do something with filePath
>> }
>>
>> Hope it helps.
>>
>> Regards,
>> rohan
>>
>> On Fri, Apr 15, 2011 at 12:40 AM, caius ligarius <caius9090 at gmail.com>wrote:
>>
>>> I have the path to some parent folder say some QString targetDir. I
>>> would like to pick up the latest file (time of creation) which begins with
>>> "stats*.txt". I think I need to use QDirIterator but I am not sure how I can
>>> use it for my specific use.
>>>
>>> Could somebody please help?
>>>
>>> Thanks,
>>> Caius
>>>
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at qt.nokia.com
>>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>>>
>>>
>>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110415/8c3b2474/attachment.html
More information about the Qt-interest-old
mailing list