SdFat Library List files with indexing

Hey, I’m trying to list the files on an SD card using the SD fat library. Unfortunately, something seems to be wrong with my printDirectory function my check if files exist seems to return false regardless of the existence of files. Any help would be greatly appreciated.

   #include<SPI.h>
    #include "SdFat.h"
    #include "FreeStack.h"
    
    // SD card chip select pin.
    const uint8_t SD_CS_PIN = 10;
    

    //**Is there a way I make an array without allocating the size here? 
    //------------------------------------------------------------------------------
    void setup() 
    {
    SdFat sd;
    SdFile dirFile;
    SdFile file;
    
    // Number of files found.
    uint16_t numberOfFiles = 0;
        
    // Position of file's directory entry.
    uint16_t dirIndex[300];
      Serial.begin(38400);
      while (!Serial) {}
          
      // Initialize at the highest speed supported by the board that is
      // not over 50 MHz. Try a lower speed if SPI errors occur.
      if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) 
      {
        sd.initErrorHalt();
      }
      if (dirFile.open("/", O_READ)) 
      {
      printDirectory(dirFile, 0);
      }
      for (int i=0; i<numberOfFiles; i++)
      {
      Serial.print(dirIndex[i]);
      Serial.print(' ');    
      }
    }
    void printDirectory (SdFile CFile, int numTabs)
    {
      SdFile file;
      while (file.openNext(&CFile, O_READ))
      {
      if (file.isHidden()||false)
        {
          //file hidden, skip
        }
        else
        {
          for (uint8_t i = 0; i < numTabs; i++) 
          {
            Serial.print('\t');
          }
          if (file.isSubDir())
          {
            SdFile SubDirFile;
            //Serial.print(file.dirIndex());
            //Serial.write(' ');
            //file.printName(&Serial);
            //Serial.println();
            if(SubDirFile.open(&CFile, file.dirIndex(), O_READ))
            {
              printDirectory(SubDirFile, numTabs+1);
            }
          }
          else 
          {
            // Save dirIndex of file in directory.
            dirIndex[numberOfFiles] = file.dirIndex();
            // Print the file number and name.
            numberOfFiles++;
            Serial.print(numberOfFiles);
            Serial.write(' ');
            file.printName(&Serial);
            Serial.println();
          }
        }
      file.close();
      }
    }
            // **THIS is returing true regardless of if there are files on the card**
            file.printName(&Serial);

file is not pointing to a file. How can you expect to print it's name?

:o Ok! That was most likely one of my problems, I rewrote the function that's now getting me the first level successfully. Could you advise me on how to get the recursion working?

Could you advise me on how to get the recursion working?

Without knowing what the problem is?

PaulS:
Without knowing what the problem is?

Sorry, the problem appears to be that I needed to initialise SdFile inside the function. The code above is working now and recursively lists files in an SD card through use of the SdFat library.