Iterating through a Directory and storing the maximium file number as an integer

Here is a very fast function to find the "maximium file number as an integer" It was developed for a user with a huge number of files so one pass through the directory was required.

#include <SD.h>
const uint8_t chipSelect = 10;
//------------------------------------------------------------------------------
// Return -2 if error, -1 not found, else max file number.
long maxFileNumber(const char* dirName, const char* baseName, const char* fileExt) {
  File dir;
  long maxNum = -1;  
  uint8_t len = strlen(baseName);

  dir = SD.open(dirName, FILE_READ);
  if (!dir || !dir.isDirectory()) return -2;
  
  while (true) {
    char *ext, *name, *ptr;
    dir_t d;
    // Return result if no more entries.
    if (dir.read(&d, 32) != 32 || d.name[0] == 0) return maxNum;
    // Only check existing regular files.
    if (d.name[0] == DIR_NAME_DELETED || !DIR_IS_FILE(&d)) continue;
    // Cast file extension to char* for ease of use
    ext = (char*)d.name + 8;
    ext[3] = 0;
    // Remove any blank fill.
    if ((ptr = strchr(ext, ' '))) *ptr = 0;
    // Skip if wrong file extension. Use case independent compare.
    if (strcasecmp(ext, fileExt)) continue;
    // Cast d.name to char* for ease of use
    name = (char*)d.name;  
    // Don't look at file extension
    name[8] = 0;      
    // Look for baseName with case independent compare.
    if (0 == strncasecmp(name, baseName, len)) {
      // Place for start of number.
      char* bgn = name + len;
      // Iterator
      char* ptr = bgn;
      long n = 0;
      // Get number.
      while (isdigit(*ptr)) n = 10*n + *ptr++ - '0';
      // make sure the end of the file name is a valid number.
      if (ptr != bgn && (*ptr == 0 || *ptr == ' ') && n > maxNum) maxNum = n;
    }
  }
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  if (!SD.begin(chipSelect)) {
    Serial.println(F("SD.begin failed"));
    while(1);
  } 
  Serial.print(F("ANALOG,BIN: "));
  Serial.println(maxFileNumber("/", "ANALOG", "BIN"));
  Serial.print(F("DATA,TXT: "));
  Serial.println(maxFileNumber("/", "DATA", "TXT"));
  Serial.print(F("SHORT,TX: "));
  Serial.println(maxFileNumber("/", "SHORT", "TX"));
  Serial.print(F("NOEXT,<null>: "));
  Serial.println(maxFileNumber("/", "NOEXT", ""));
  Serial.print(F("<nobase>,TXT: "));
  Serial.println(maxFileNumber("/", "", "TXT"));
  Serial.print(F("MISSING,TXT: "));
  Serial.println(maxFileNumber("/", "MISSING", "TXT")); 
   Serial.print(F("<bad dir>: "));
  Serial.println(maxFileNumber("/BADDIR", "DATA", "TXT")); 
 
  Serial.println(F("Done!"));
}
void loop() {}

I ran the program on an SD that had these files root directory:

12345678.TXT
ANALOG00.BIN
ANALOG01.BIN
ANALOG02.BIN
ANALOG03.BIN
ANALOG04.BIN
ANALOG05.BIN
ANALOG99.TXT
DATA000.TXT
DATA999.TXT
NOEXT00
NOEXT09
SHORT05.TX
TEST.CSV

Here is the test output:

ANALOG,BIN: 5
DATA,TXT: 999
SHORT,TX: 5
NOEXT,: 9
,TXT: 12345678
MISSING,TXT: -1
: -2
Done!

Note: Finding "12345678.TXT" by opening every possible file in the series would take close to forever.