title: listing files on arduino SD
body: <p>I have been trying to run Arduino file dump and directory listing examples together and hit a wall.</p>
  
  <p>With the sketch below, if I attempt to dump a file (even if the file doesn't exist on the SD card) and then do a directory listing of the SD the sketch returns "** no more files **". I think this must be due to something being left open but for days have not been able to find where. Any help would be much appreciated.</p>
  
  <p>Sketch:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;SD.h&gt;
  
  Sd2Card card;
  SdVolume volume;
  File root;
  String inputString = "";
  char FName[12];
  
  const int chipSelect = 10;
  
  void setup(){
    Serial.begin(9600);
    Serial.print("\nInitializing SD card...");
    pinMode(10, OUTPUT);
    if (!SD.begin(chipSelect)) {
      Serial.println("initialization failed.");
    } else { Serial.println("SD Card OK"); }
    /*  if (!volume.init(card)) {
      Serial.println("No Partition Format SD"); return; }*/
  }
  
  void loop(void) {
    getSerialString();
    if (inputString == "id") {
      Serial.println("SD Card Test"); 
    } else {
      if (inputString == "fr") {
        Serial.println("File to Read");
        getSerialString();
        inputString.toCharArray(FName, inputString.length()+1);
        if (!dumpFile(FName)) {
          Serial.print("error opening "); 
          Serial.println(FName); 
        }
      } else {
        if (inputString == "dir" ) {
          File root = SD.open("/");
          if (root ) { 
            printDirectory(root, 0); 
            root.close();
          }
        }
      }
    }
    inputString = "";
  }
  
  boolean dumpFile(char fileNm[]) {
     File dataFile = SD.open(fileNm);
     if (dataFile) {    // if the file is available, read from it:
       while (dataFile.available()) { 
         Serial.write(dataFile.read()); 
       }
       dataFile.close();
     } else {dataFile.close(); return false; }
  }
  
  void printDirectory(File dir, int numTabs) {
    while(true) {
       File entry =  dir.openNextFile();
       if (! entry) {
         // no more files
         Serial.println("**no more files**");
         break;
       }
       for (uint8_t i=0; i&lt;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();
      }
    }
  }
  
  void getSerialString(void) {
    char inChar = ' '; inputString = "";
    // Serial.println("Waiting for User");
    while (inChar != 10) {
      while (Serial.available()) {
        inChar = Serial.read();     // get the new byte:
        if (inChar != 10) {         // add it to the inputString:
          inputString += inChar;
        }
      }
    }
  }
  </code></pre>