Getting the highest index in folder
#1

Let's say I have the following files in my directory:

Quote:

file_1
file_24
file_99
file_3
file_13

How can I get the highest index of that folder? In this case I'd want to get the "file_99" file.
Reply
#2

pawn Код:
new File: my_FILE,
      str[15],
      temp;
for( new i = 0; i < MAX_FILES; i++ )
{
     format(str, 15, "file_%d", i);
     my_FILE = fopen( str, io_read );
     if( my_FILE )
     {
           temp = i;
     }
     else continue;
}

MAXIMUM INDEX  =  temp;
I think something like this COULD work? I am not too experienced with files, TBH.
Reply
#3

Fixed, thank you Rajat.
Reply
#4

It's bad practice to store files, which should be indexed, like this. We don't know how many files do exist, so you can basicly create a loop going to 2^31, and make yourself coffee (and wait hours or days), while your script still searches for the highest "indexed file" in your directory.

You can use databases, which are just the best option to use for such purposes. Alternatively you can create a table of file names, where you can keep track of all the files inside this directory.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)