Go Down

Topic: Read filename from SD card, store to a string (Read 2 times) previous topic - next topic

WanaGo

Hello,

Been scratching my head for a little while, I cant seem to figure out how to read a file name of a file I have on SD card, and save the file name (not its contents, just the file name itself) to a string.
What I am trying to do is build an array of file names. I am using the SDFAT library.

Better yet, the files are actually numbers, ranging from 0001.mp3 to 9999.mp3

Can someone point me in the right direction for reading the file name, say 0001.mp3, and saving this an int variable. So it will be 1 to 9999.

Thanks in advance.

WanaGo

#1
Nov 06, 2012, 11:20 am Last Edit: Nov 06, 2012, 11:31 am by WanaGo Reason: 1
I found this:

Code: [Select]
//------------------------------------------------------------------------------
/** Get a file's name
*
* \param[out] name An array of 13 characters for the file's name.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
bool SdBaseFile::getFilename(char* name) {
 dir_t* p;
 if (!isOpen()) {
   DBG_FAIL_MACRO;
   goto fail;
 }
 if (isRoot()) {
   name[0] = '/';
   name[1] = '\0';
   return true;
 }
 // cache entry
 p = cacheDirEntry(SdVolume::CACHE_FOR_READ);
 if (!p) {
   DBG_FAIL_MACRO;
   goto fail;
 }
 // format name
 dirName(*p, name);
 return true;

fail:
 return false;
}


However this is big boys code, and I cant understand what it means exactly, where the name of the file actually goes...

Quote
* \param[out] name An array of 13 characters for the file's name.


what is this param[out] exactly?

or is that wrong...

Is it if I start at the begining of the directory, and call that function, it will tell me yes/no if the file exists...

ie
boolean SongPresent = getFilename("0001.mp3");

?

I could possibly make that work, but ideally I want to start at the beginning of the root directory, and scan through all the files on the card, returning the name of the file, ideally just the name, saving it into an int array.

hmm. The search continues.

*EDIT* Actually, this would be better for that example

Quote
//------------------------------------------------------------------------------
/** Test for the existence of a file in a directory
*
* \param[in] name Name of the file to be tested for.
*
* The calling instance must be an open directory file.
*
* dirFile.exists("TOFIND.TXT") searches for "TOFIND.TXT" in  the directory
* dirFile.
*
* \return true if the file exists else false.
*/
bool SdBaseFile::exists(const char* name) {
  SdBaseFile file;
  return file.open(this, name, O_READ);


Not really sure what the one above is for.

WanaGo

Currently I do this

Code: [Select]
// open next file in root.  The volume working directory, vwd, is root
  while (sdCardFile.openNext(sdCard.vwd(), O_READ))
{
  MaxFiles++;
   
  sdCardFile.close();
}


which is mostly extracted from one of the examples.
So it just increments through all the files in root, and adds 1 to the count. I then get the total file count out at the end.

Its this I want to extend on, so not only get the file count, but get a list of ints, of all the files present in root.

Cheers

WanaGo

Essentially I want this:

Code: [Select]
//------------------------------------------------------------------------------
/** Print a file's name
*
* \param[in] pr Print stream for output.
*
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
bool SdBaseFile::printName(Print* pr) {
  char name[13];
  if (!getFilename(name)) {
    DBG_FAIL_MACRO;
    goto fail;
  }
  return pr->print(name) > 0;

fail:
  return false;
}


But I dont want to print it to the serial port...

:~

Really showing the gaps in my knowledge here.

PaulS

Quote
I cant seem to figure out how to read a file name of a file I have on SD card, and save the file name (not its contents, just the file name itself) to a string.

Nor, it seems, can you figure out how to post your code.

Quote
Can someone point me in the right direction for reading the file name, say 0001.mp3, and saving this an int variable. So it will be 1 to 9999.


Code: [Select]
int num = 0;
while(file.available() > 0)
{
   char ltr = file.read();
   if(ltr >= '0' && ltr <= '9')
   {
      num *= 10;
      num += ltr - '0';
   }
   else // found the ., the m, the p, the 3, the <cr>, or the <lf>
   {
      // Save num somewhere

      num = 0;
   }
}

Go Up