Listing Files

Once you are reliably reading SD card information, you can move on to looking at what files might be on the SD card. The code below is the Arduino SD Card Example Listfiles. It is modified to put the WIZ5500 chip select high so the SPI bus is only talking to the SD card.

At this point, please place a few files on the SD card from your computer to get an interesting output rather than using a blank card. It is suggested to put two or three text files with the filenames being no more than 8 characters before the period then end in .txt. An example would be README.txt or todo.txt. You can put the code text files on the card also, having them end in .ino will be fine also.

/*  SDlistFiles

 This example shows how print out the files in a directory on a SD card

 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 Adafruit #2971 and Metro/Uno

 created   Nov 2010 by David A. Mellis
 modified 9 Apr 2012 by Tom Igoe
 modified 2 Feb 2014 by Scott Fitzgerald
 modified 12 Apr 2018 by Mike Barela 

 This example code is in the public domain.
 
 */
#include <SPI.h>
#include <SD.h>

File root;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(10, OUTPUT);      // set the SS pin as an output (necessary!)
  digitalWrite(10, HIGH);   // but turn off the W5100 chip
  
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
}

void loop() {
  // nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs) {
  while (true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      break;
    }
    for (uint8_t i = 0; i < numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1);
    } else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }
}

The screen shot below shows information on the same card as in the last sketch but lists the files and their sizes.

Note in Windows, filenames like WPSETT~1.DAT refer to a longer filename (such as WPSETTINGS.DAT). Microsoft, for compatibility with older software, truncates filenames greater than 8 characters (not including the 3 character file extension) at 6 characters, a tilde (~) character, and a number (in case multiple truncated files exist). The older software cat read and write to the file the same as an 8.3 character file.

The extra information for the long file name is hidden from older software but perfectly safe for newer software. There are no worries opening the shortened filename will somehow corrupt the capability to use the long filenames as long as using the file name per the name you see (and not changing the file name) is observed.

This guide was first published on Apr 20, 2018. It was last updated on Apr 20, 2018. This page (Listing Files) was last updated on Jun 17, 2019.