Author Topic: The forget? - findNextFile  (Read 1048 times)

What in the name of god is wrong with this function? It works and it doesn't. If I do this manually in console, it works fine:

Code: [Select]
==>echo(findNextFile($loc));
[...]/icon_silverStar.png
==>echo(findNextFIle($loc));
[...]/icon_silverBadge.png

However, if I run it through a for loop..
Code: [Select]
==>for(%i=0;%i<getFileCount($loc);%i++) { echo(findNextFile($loc)); }
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png
[...]/icon_silverStar.png

Now, I'm pretty damn sure that says "find next file" so why is it returning the first file every single time?

Try storing getFileCount to a variable to use for the for loop.

What in god's name? Why did that fix it. I'm beyond confused. getFileCount just returns 8, why would storing 8 fix it?

Anyway, thanks.


here's a method that uses while loops, fwiw

Code: [Select]
function servercmdlistfiles(%c,%path,%extension)
{
if(!%c.issuperadmin)
return;
%a = -1;
%file[%a++] = findFirstFile(%path @ "/*." @ %extension);
while (strlen(%file[%a]) > 0)
{
%file[%a++] = findNextFile(%path @ "/*." @ %extension);
messageClient(%c, %file[%a]);
}
}

Yeah, I get that there are other ways to do it. I had it working before I posted the topic I just don't understand why you have to store the variable as that makes absolutely no sense to me.

Yeah, I get that there are other ways to do it. I had it working before I posted the topic I just don't understand why you have to store the variable as that makes absolutely no sense to me.
It restarts the findNextFile() loop.

here's a method that uses while loops, fwiw

Code: [Select]
function servercmdlistfiles(%c,%path,%extension)
{
if(!%c.issuperadmin)
return;
%a = -1;
%file[%a++] = findFirstFile(%path @ "/*." @ %extension);
while (strlen(%file[%a]) > 0)
{
%file[%a++] = findNextFile(%path @ "/*." @ %extension);
messageClient(%c, %file[%a]);
}
}
This is really a for loop in disguise, and does tons of unneeded allocations. Better version:
Code: [Select]
while(%file = findFirstFile(%path) ; %file !$= "" ; %file = findNextFile(%path)) {
    // Do stuff
}

You need a findFirstFile

This is really a for loop in disguise, and does tons of unneeded allocations. Better version:
Code: [Select]
while(%file = findFirstFile(%path) ; %file !$= "" ; %file = findNextFile(%path)) {
    // Do stuff
}
you mean a for() loop, right?


Store the find next file/find first file in a variable, it helped me