Pages: [1] 2   Go Down
Author Topic: Read filename from SD card, store to a string  (Read 2443 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this:

Code:
//------------------------------------------------------------------------------
/** 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.
« Last Edit: November 06, 2012, 05:31:02 am by WanaGo » Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Currently I do this

Code:
// 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
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Essentially I want this:

Code:
//------------------------------------------------------------------------------
/** 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...

 smiley-confuse

Really showing the gaps in my knowledge here.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
   }
}
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Um... my code has absolutely nothing to do with this Paul.
If I wanted to post my code I would have done so, but it has absolutely no relevance to this question.

Thanks for the snippet you posted, ill digest it and see if its what I need.

Regards

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Um... my code has absolutely nothing to do with this Paul.
If your code has nothing to do with reading file names from the SD card, why are you trying to do it?
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you want to see my MP3 player code, when its not related to the question I am asking.
I could have a new project with no code, and I want to read the file name using the SDFAT library, and there would be no code to post.

The only thing relating to opening a file is the part I posted above, which is directly out of the example from the SDFAT library, I have gotten no where further than that.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why do you want to see my MP3 player code, when its not related to the question I am asking.
Because I assumed that you had tried something, and failed, or hit a wall, or something. So I was wrong. Sue me.
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-confuse

Not enough sleep at my end I think.

Everything I have tried I have posted. All I am trying to do is find the function or the method to read the file name using the sdfat library.

I have just searched the sdfat library and cant find a function called available()

Quote
while(file.available() > 0)

Where is this from?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I could have a new project with no code, and I want to read the file name using the SDFAT library, and there would be no code to post.

If that was the case, you would have been wasting everyone's time by asking for help before you had made any attempt to solve the problem for yourself.

If this problem is a small part of a complex sketch then write a new sketch which demonstrates the problem in the simplest possible way. You will find this often helps you to make progress on the problem yourself since it forces you to test assumptions about which parts of your code are relevant to the problem.

In this case you want to open the directory (using SD.open()), access each file in turn (using File.openNextFile()) and get the file name (using File.name()).

If you know the file name includes a decimal number and want to know what the number is, you need to parse the number from the string. The C runtime library provides various functions such as atoi() to do that sort of thing.

This does not look terribly difficult and I would have thought you could at least have a stab at it before you give up and ask for help. Showing us your attempt would also show us which parts of this problem you have solved and which parts are causing you trouble, so the people trying to help you aren't left trying to solve the whole problem for you from scratch, which seems to be the situation at the moment.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have this, which I posted above

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

And I essentially want to do something like this:

Code:
// open next file in root.  The volume working directory, vwd, is root
  while (sdCardFile.openNext(sdCard.vwd(), O_READ))
{
  MaxFiles++;
  sdCardFile.printName(??) //But I dont want to print to a serial stream, I want to print to a int, or a string
   
  sdCardFile.close();
}

Cheers
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I appreaciate that, but I have posted what I tried.

I didnt just go and write code I know wouldnt work, I tried looking at the sdfat library first to find a function which was suitable, but I havent found one that I understand how to use, hence the post...

There is no more code to post as I havent written any....

Im confused why this is so much of an issue... should I be trying stuff I have no idea if it will work, or should I be reading the library to find something suitable first. I thought the 2nd option was the valid option here...

Sorry if I have offended anyone, but I just havent written non working code, would like to find a viable solution from the library first...
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In this case you want to open the directory (using SD.open()), access each file in turn (using File.openNextFile()) and get the file name (using File.name()).

This is what I am essentially doing, but I cant find the name() you are speaking of...
Logged

NZ
Offline Offline
Sr. Member
****
Karma: 0
Posts: 390
Turtle in a hard shell
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Crap ok, found it.

I was using these:

//Add the SdFat Libraries
#include <SdFat.h>
#include <SdFatUtil.h>

not the base SD library.

I see available() now too.

Thanks
Logged

Pages: [1] 2   Go Up
Jump to: