title: Arduino SD card select delete
body: <p>I am trying to make a sort of a menu to allow the user to delete a file or multiple files from an <a href="http://en.wikipedia.org/wiki/Secure_Digital" rel="nofollow">SD card</a>.</p>
  
  <p>I can get a full list of all the files easy enough, but when it comes to running through and asking the user if each file much be deleted, I somehow only get the first seven files and then the seventh on is repeated until the max number of file are reached.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;SD.h&gt;
  
  File datafile;
  
  void setup()
  {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      Serial.print("Initializing SD card...");
      pinMode(10, OUTPUT);
  
      if (!SD.begin(10)) {
          Serial.println("initialization failed!");
          return;
      }
      Serial.println("initialization done.");
      Serial.println("done!");
  }
  
  void loop()
  {
      while (true)
      {
          char c;  // A choice var this will be changed later to a button so for now this method works
          datafile = SD.open("/");
          printDirectory(datafile);  // Function to print the full list of files in one go
          Serial.println("Do you want to delete a file? (y = 1 / n = 2)");
  
          while(!Serial.available()) // Wait until the user inputs something.
          {}
          c = Serial.read();
          if (c=='2')
              hold();                // If no, go to hold function ( basically end program )
          else
              if (c=='1')            // If yes, go to the delete function
              {
                  datafile = SD.open("/");  // Give the data link to the SD card
                  deletnum(datafile);
              }
              else
                  Serial.println("Choice not correct try again");  // If choice is wrong, I'm not too concerned about this now.
      }
  }
  
  void deletnum(File dir)
  {
      Serial.println("Scrolling numbers now");  // Scroll though the numbers and pick what to delete
  
      int c=0;
      File entry;
      int x=1;
      while(true)
      {
          entry =  dir.openNextFile();
          // if (! entry)
          // { // no more files
          //   break;}
          Serial.print("This one ? 1=yes , 2= no : ");
          Serial.println(entry.name());
          while(!Serial.available())
          {;}
  
          c = Serial.read();
          if(c=='1')                             // If they picked yes then delete it and go to the next file and ask.
          {
              SD.remove(entry.name());
              Serial.println(" file deleted")
          }
          else
              if (c=='2')
                  Serial.println("not that one then");
      }
  }
  
  void printDirectory(File dir)   // Function to print the full list of files in one go
  {
      int x=1;
      while(true)
      {
          File entry =  dir.openNextFile();
          if (! entry)
          { // No more files
              break;
          }
          Serial.print(x);
          Serial.print(") ");
          Serial.println(entry.name());
          x++;
      }
  }
  
  void hold() // A function just to "hold" or stop the program if he user does not want to delete any more files.
  {
      while(true)
      {
        Serial.println("Holding");  // here just to show me that it is in the loop
      }
  }
  </code></pre>
  
  <p>But after all of that I get this output:</p>
  
  <pre><code>Initializing SD card...initialization done.
  
  done!
  
  1) TEMPDATA.TXT
  
  2) DTAD.TXT
  
  3) 84.TX
  
  4) 104.TX
  
  5) TEMPDA00.TXT
  
  6) TEMPDA02.TXT
  
  7) TEMPDA03.TXT
  
  8) TEMPDA04.TXT
  
  9) TEMPDA05.TXT
  
  Do you want to delete a file ? (y = 1 / n = 2)
  
  Scrolling numbers now
  
  This one ? 1=yes , 2= no : TEMPDATA.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : DTAD.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : 84.TX
  
  not that one then
  
  This one ? 1=yes , 2= no : 104.TX
  
  not that one then
  
  This one ? 1=yes , 2= no : TEMPDA00.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : TEMPDA02.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : TEMPDA03.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : TEMPDA04.TXT
  
  not that one then
  
  This one ? 1=yes , 2= no : TEMPDA04.TXT      // Here lies the problem. This should be TEMPDA05.TXT.
  
  not that one then
  
  This one ? 1=yes , 2= no :     // There are not more files so there are no more names.
  </code></pre>
  
  <p>This happens if I have more files as well, but it always stops at seven and then repeats. Why?</p>