[SD] How can I print the directory name from which we list the content

Good evening and happy new year!!!

I am working on a code to list the file of a directory of my SD card.

I inspirated my self with that exemple

I call my function like this:

 logfile = SD.open(gpsLogFolder);
        printDirectory(/GPS/, 0);
        SerialUSB.println("done!\n");
        logfile.close();

Below, there is the code of my function printDirectory().

In first argumet, I enter the director to list. In my case is /GPS/.
In that folder, there are other child folders with a file.

2015/12/21/20-33-31.TXT

I also would like to get your attention with this part of the printDirectory function

sprintf(sd_path, "%s", gpsLogFolder);

gpsLogFolder is a char variable and it’s value is “/GPS/”. I defined it before the setup() function.

I would like to replace the variable gpsLogFolder with the value of dir.name() (see below).

If you look at the begining of printDirectory() function, there is that code

SerialUSB.println(dir.name());

That code print this

/
2015
12
21

That’s nice but it not what I need. I would like to have

/GPS/
2015
12
21

How can I adapt my code printed the name of defined folder to be listed?
In my case, I defined /GPS/, but if I do it

 logfile = SD.open(gpsLogFolder);
        printDirectory(/GPS/2015/, 0);
        SerialUSB.println("done!\n");
        logfile.close();

I would like to have printed

/GPS/2015/
12
21

Here is my printDirectory() code

const char gpsLogFolder[] = "/GPS/";
#define DEEPTHPATH 4
char * path[DEEPTHPATH-1];

void printDirectory(File dir, int numTabs) {
 if(isSdReady == true)
 {
//DEBUG
  SerialUSB.println(dir.name());
  
  if(numTabs == 0)
  {
    memset(sd_path, '\0', sizeof(sd_path));
  }

    while(true)
    {
      File entry =  dir.openNextFile();
      if (! entry)
      {
        // no more files
         // DEBUG
        SerialUSB.print(F("Nb of Files: "));
        SerialUSB.println(countSavedFilesInSd);
        SerialUSB.print(F("\n"));
      
        break;
      }
     
      for (uint8_t i=0; i<numTabs; i++)
      {
       //SerialUSB.print('\t');
      }
     
      // SerialUSB.print(entry.name());

     
      if (entry.isDirectory())
      {
        char *p = entry.name();
        path[numTabs] = p;

        countSavedFilesInSd = 0;
      
        // SerialUSB.println("/");
       
        printDirectory(entry, numTabs+1);
        
      }
      else
      {
        sd_path[0] = '\0';
//        sprintf(sd_path, "%s", gpsLogFolder);
strcpy(sd_path, gpsLogFolder);
sd_path[strlen(gpsLogFolder)] = '\0':
       
        for(int f=0; f < numTabs; f++)
        {
          strncat(sd_path, path[f], strlen(path[f]));
          strncat(sd_path, "/" , 1);
        }
 

        memset(sd_path_file,'\0',sizeof(sd_path_file));
        sprintf(sd_path_file, "%s%s", sd_path,entry.name());
       
       
        // files have sizes, directories do not
        SerialUSB.print("\t");
        SerialUSB.println(entry.size(), DEC);
        // Count the bumber of files
        countSavedFilesInSd++;

     }
     
     entry.close();
   
   }
 }  
}

Thank for your help!

gpsLogFolder is a char variable and it’s value is “/GPS/”.

No, it isn’t. It might be a char ARRAY.

It does not make sense to use sprintf() to perform a strcpy().

      for (uint8_t i=0; i<numTabs; i++)
      {
       //SerialUSB.print('\t');
      }

Certainly looks useful…

      // SerialUSB.print(entry.name());
     
      /*
      SerialUSB.print("(");
      SerialUSB.print(numTabs);
      SerialUSB.print(")");
      */

More useless crap. Why do you expect us to wade through all this commented out code? Use your delete key!

You recursively call printDirectory(). You would need to completely restructure your code to get rid of recursion to do what you seem to want to do.

Dear Pauls,

Thank for your reply but it does not really answer to my queston.

I removed this

// SerialUSB.print(entry.name());
     
      /*
      SerialUSB.print("(");
      SerialUSB.print(numTabs);
      SerialUSB.print(")");
      */

I kept commented the orginal code

My pasted code is a copy of the Arduino exemple
As this

for (uint8_t i=0; i<numTabs; i++)
      {
       //SerialUSB.print('\t');
      }

Come from the example.

I juts do not want to have the result printed on the Serial Monitor, but I want to store the files loction into a variable named ‘sd_path_file’. But this is not my issue/worries. No need to spend time on it.

gpsLogFolder is declared as this

const char gpsLogFolder[] = "/GPS/";

My worries is how to work with ‘File dir’ (first parameter of printDirectory()

void printDirectory(File dir, int numTabs){
SerialUSB.println(dir.name());
}

If I list the files of /GPS/ folder which contain other folder as
/GPS/2015/12/23/

SerialUSB.println(dir.name());

will print

/
2015
12
23

and I would like to have

/GPS/
2015
12
23

How can have printed the folder from which we want to list the content, in order to have /GPS/ instead of /

How can have printed the folder from which we want to list the content, in order to have /GPS/ instead of /

If the file/folder being passed in is /GPS (not /GPS/), then I can't see how you get the output that you show. The output should be /GPS on the first line, and each folder or file name on subsequent lines.