greiman / SdFs

Arduino FAT16/FAT32/exFAT Library
MIT License
48 stars 15 forks source link

Dir listing #2

Open Misfittech opened 6 years ago

Misfittech commented 6 years ago

I tired to do a directory listing on exfat and it seems to fail. I can open the SD card, but the root "/" getName() returns '\0', then openNext() returns false.

bool ret=SD.begin( PIN_SD_CS); if (ret == false) { ERROR("SD error %d",ret);//,TO_MHZ(48)));//,,&SPI2)); }

    FsFile dir,f;
    char str[50];
    dir=SD.open("/");
    dir.getName(str,50);
    LOG("%s",str);
    uint32_t i=0;
    while (dir.openNext(&f) && i<170)
    {
        f.getName(str,50);
        LOG("%s",str);
        i++;
    }
    dir.close();
    f.close();
greiman commented 6 years ago

See the OpenNext example. Here is part of that example.

You can replace the printName() with getName().

  // Open root directory 
  if (!dir.open("/")){
    error("dir.open failed");
  }
  // Open next file in root.
  // Warning, openNext starts at the current position of dir so a
  // rewind may be necessary in your application.
  while (file.openNext(&dir, O_READ)) {
    file.printFileSize(&Serial);
    Serial.write(' ');
    file.printModifyDateTime(&Serial);
    Serial.write(' ');
    file.printName(&Serial);
    if (file.isDir()) {
      // Indicate a directory.
      Serial.write('/');
    }
    Serial.println();
    file.close();
  }
  Serial.println("Done!");

You may want to read the documentation in the doc folder for member functions. Here is openNext().

bool ExFatFile::openNext ( ExFatFile * dirFile,uint8_t oflag = O_READ )

Open the next file or subdirectory in a directory.

Parameters [in] dirFile An open instance for the directory containing the file to be opened. [in] oflag bitwise-inclusive OR of open mode flags. See see open(ExFatFile, const char, uint8_t).

Returns true for success or false for failure.