Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10
Accepted Solution

QDirIterator skipping last file

[ Edited ]

I'm working with QDirIterator to get a list of files in a directory, there's some issues i'm having though.

 

Ultimately it doesnt list the very last file,

It starts by listing the file just before the one it doesnt list.  So say there's 10 files, it starts with #9 then goes from 1-8 and 10 just gets left out.

 

Is there any reason it's omitting this file and starting the list with the file before it?

 

my code is

 

void ApplicationUI::iterator()
{
	qDebug() << "Iterator";
	QDir dirPath = QDir(QDir::homePath() + "/shared/music");
	
	QDirIterator myDirIt2(dirPath.absolutePath(), QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
	qDebug() << "dirPath2: " << dirPath.absolutePath();

        QDir mDir = QDir(myDirIt2.filePath());
        mDir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
        qDebug() << mDir.dirName() << " file count " << mDir.count();

	while (myDirIt2.hasNext()) {
		if (myDirIt2.fileInfo().isFile()){
			qDebug() << "File Name: " << myDirIt2.fileName() << " - File Path: "  << myDirIt2.filePath();
		}
		myDirIt2.next();
	}
}

 

I have ruled out it being the file size, or just the file by adding another file making the count 11, it started with 10 (the one it wouldn't show before) and then went from 1-9 leaving out 11

Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

[ Edited ]

Yep, poor logic.

 

You are checking for the next folder but then doing the search before moving to it, so your for loop will always end one early.

So when you get to 10 you are doing the next statement moving it to 11 but then the while loop expression is satisfied and finishes not getting to your qDebug statement.

 

Changing it to a do...while loop would be the easiest change.

 

You should also have an outer if statement checking if any directories exist at all.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: QDirIterator skipping last file

I changed it to a do {} while();  and now it provides the last result but still starts with the one prior 

 

so it goes from 9 then 1-8, & 10.

 

I haven't implemented the folder check because i'll be creating my own folder and implementing it then, but wanted to see if this would even work before i got that far.

 

I have found a working alternative method of doing this, by using entryInfoList, but I still find it odd that QDirIterator is presenting the results out of order.

Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

It may be the order returned is not the one you think it should be, there is nothing in the docs I've quickly scanned to say it is alphabetical it could be disk order, date order, etc.

 

Try a different folder and see if you can work out what it is.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

Read through too fast, in fact there is no sorting...

 

   "Unlike QDir::entryList(), QDirIterator does not support sorting."


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: QDirIterator skipping last file

yeah but that's where the reason i find it's sorting as odd... i just dropped all the files in there at the same time named 01, 02 etc and it always gives me the one before the last first.  and even if i add a file it sticks to this sorting always proving the file before the last

 

Since we can't filter the sorting it should provide it in a logical order, but for some reason it's just a tad bit off

Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

Depends what you mean by a logical order, a memory designer will tell you that the most efficient order to access memory in depends on the memory type you are using.

 

In short if you want a logical order that you can control use one of the other classes or sort the list yourself first.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

Unix file systems used to be a required subject in my Uni days and it was a fairly long course.

So if you are interested in delving deeper there are lots of resources out there that would explain why.

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: QDirIterator skipping last file

Well thanks for the help =) , now that I discovered entryInfoList I'm just going to stick with it.

 

Prior that discovery my intention was to use this iterator to map the file path and then use the data that was mapped out but I suppose it's just not meant to be done that way

Developer
BBSJdev
Posts: 6,118
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30

Re: QDirIterator skipping last file

Thats right you'd probably use this class more for doing system level operations where you don't care about the order just the speed at which you can walk through them.


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.