SD Fat openNext exists how to openPrevious

I'm using the SD Fat Library and would like to write a function that will open the next or previous file (in lexigraphic order) with a .csv extension. For example with a file tree of

file1.txt file2.csv folder1 file3.csv folder2 file4.txt file5.csv file6.csv

the open next function when called would return file2.csv, file3.csv, file5.csv, file6.csv, and repeat from file2.csv. while the previous file function would return the file previous to the last file called.

What would be the best way to go about this?

Right now I have the following code that handles going forward but is not recursive

    void SDBrowse (char *Disp, int movement)
    {
      char SFN[13];
      SdFile BrowseFile;
      while(movement>0)
      {
        if(!BrowseFile.openNext(&dirFile, O_READ)){Serial.println("error");break;}
        BrowseFile.getName(Disp,17);
        BrowseFile.getSFN(&*SFN);
        Serial.println(Disp);
        if(strstr(SFN,".CSV") != NULL){movement--;}
        BrowseFile.close();
      }
    }

Do you mean next and previous in lexigraphic order, i.e. regardless of how the files are named?

I do, yes

Right now I have the following code that handles going forward but is not recursive

    void SDBrowse (char *Disp, int movement)
    {
      char SFN[13];
      SdFile BrowseFile;
      while(movement>0)
      {
        if(!BrowseFile.openNext(&dirFile, O_READ)){Serial.println("error");break;}
        BrowseFile.getName(Disp,17);
        BrowseFile.getSFN(&*SFN);
        Serial.println(Disp);
        if(strstr(SFN,".CSV") != NULL){movement--;}
        BrowseFile.close();
      }
    }

You will have to maintain your own file list which you obtain by opening and closing all the files in the directory, as in the List File example of the SD library. Then you can open or close them from the list that you built, as desired.

ATEENGE: but is not recursive

That's probably a good thing recursive code can get really messy. Functions should only call themselves when ABSOLUTELY necessary and most of the time there is a good way around it.

Or did you have some other meaning for the word recursive?