Long file name reading with SdFat

Hello,

I’m looking for a function to read the long file name of a file that I just opened.
I found a function in another thread that allows me to read the LFN of the next file :

dir_t dirLfnNext( char * lfn )
{
  uint8_t offset[] = {1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30};
  dir_t dir;
  uint8_t lfnIn = 130;
  uint8_t i;
  uint8_t ndir;
  uint8_t sum;
  uint8_t test;
  bool haveLong = false;

  while( dirFile->read( &dir, 32 ) == 32 )
  {
    if( DIR_IS_LONG_NAME( &dir ) )
    {
      if( ! haveLong )
      {
        if(( dir.name[0] & 0XE0 ) != 0X40 )
          continue;
        ndir = dir.name[0] & 0X1F;
        test = dir.creationTimeTenths;
        haveLong = true;
        lfnIn = 130;
        lfn[ lfnIn ] = 0;
      }
      else if( dir.name[0] != --ndir || test != dir.creationTimeTenths )
      {
        haveLong = false;
        continue;
      }
      char *p = (char*) & dir;
      if( lfnIn > 0 )
      {
        lfnIn -= 13;
        for( i = 0; i < 13; i++ )
          lfn[lfnIn + i] = p[offset[i]];
      }
    }
    else if( DIR_IS_FILE_OR_SUBDIR( &dir ) 
             && dir.name[0] != DIR_NAME_DELETED 
             && dir.name[0] != DIR_NAME_FREE
             && dir.name[0] != '.' )
    {
      if( haveLong )
      {
        for( sum = i = 0; i < 11; i++ )
           sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + dir.name[i];
        if( sum != test || ndir != 1 )
        haveLong = false;
      }
      if( haveLong )
      {
        for( i = 0; lfnIn + i <= 130 ; i++ )
          lfn[i] = lfn[lfnIn + i];
        return dir;
      }
      // else if( dir.reservedNT )
      //  return "Reserved NT";
      else
      {
        SdFile::dirName( dir, lfn );
        return dir;  
      }
    }
    else
    {
      if( dir.name[0] == DIR_NAME_FREE )
        break;
      haveLong = false;
    }
  }
  lfn[ 0 ] = 0;
  return dir;
}

But I’m having a hard time understanding how it works exactly, and I’ve been trying to modify it to read the LFN of the file I just opened (with file.open). Sadly, it is more complicated than I expected and I’m seeking for some help here.
A function like file.getLongFilename() would really be awesome in this lib… :slight_smile:

Thank you !

Hi ViviKFF
Did you read this post?

You can loop into a directory until you find the long name (or the short name) of the file/subdirectory you are looking for.
Then you can open it using his short name and the normal functions of the library.