how to store sd dir path into string array

charnjit

Well-known member
i have A folder "AUDIO" on sd root. 10 folders in "AUDIO" folder naming "B_0","B_1","B_2","B_3"....."B_9" each folder contain 16 .wav files. wav files name start with number like

Code:
-root-----
  |----B_0
  |     |-01FlBeat.wav
  |     |-02DrRoll.wav
  |     |-03DrRoll.wav
  |     |-04HiHat.wav
  |     |-.......
  |     |-16EndHit.wav
  |
  |----B_1
  |     |-01xxxxxx.wav
  |     |-02xxxxxx.wav
  |     |-03xxxxxx.wav
  |     |-04xxxxxx.wav
  |     |-.......
  |     |-16xxxxxx.wav
  |
  |----B_2
  |     |-01xxxxxx.wav
  |     |-02xxxxxx.wav
  |     |-03xxxxxx.wav
  |     |-04xxxxxx.wav
  |     |-.......
  |     |-16xxxxxx.wav
  |  |----B_3
  |     |-01xxxxxx.wav
  |     |-02xxxxxx.wav
  |     |-03xxxxxx.wav
  |     |-04xxxxxx.wav
  |     |-.......
  |     |-16xxxxxx.wav
  |
so on


in begining try i wrote code to store paths into array to be use in SD.h library
C++:
 String B_path[10];
 String Str_A = "\AUDIO\B_";
 String Str_B = "\";
 void setup() {
  Serial.begin(9600);
 for (int i=0; i<10; i++)  {
  B_path[i]= Str_A + String(i) + Str_B;
    Serial.println(B_path[i]);
  }
  }
  void loop() {
    }
i hope result may be
\AUDIO\B_0\
\AUDIO\B_1\
\AUDIO\B_2\
\AUDIO\B_3\
\AUDIO\B_4\
\AUDIO\B_5\
\AUDIO\B_6\
\AUDIO\B_7\
\AUDIO\B_8\
\AUDIO\B_9\
but failed i seemed i used wrong code....got error
C++:
C:\Users\acer\Documents\Arduino\Ishaan_Toys\Ishaan_Toys.ino:6:17: warning: missing terminating ' character
    6 |  String Str_B = '\';
      |                 ^
Ishaan_Toys:6: error: missing terminating ' character
    6 |  String Str_B = '\';
      |                 ^~~~
C:\Users\acer\Documents\Arduino\Ishaan_Toys\Ishaan_Toys.ino:5:17: warning: unknown escape sequence: '\A'
    5 |  String Str_A = "\AUDIO\B_";
      |                 ^~~~~~~~~~~
C:\Users\acer\Documents\Arduino\Ishaan_Toys\Ishaan_Toys.ino:5:17: warning: unknown escape sequence: '\B'
Ishaan_Toys:7: error: expected primary-expression before 'void'
    7 |  void setup(void) {
      |  ^~~~
Ishaan_Toys:15: error: expected ',' or ';' before 'void'
   15 |   void loop() {
      |   ^~~~
missing terminating ' character
how to assign to B_path[i] correctly . to used to open specific folder in SD library ???
 
A single backslash '\' is being interpreted as an escape sequence. Maybe try using double backslash '\\' as follows (i'm away from my Teensy boards, so this is compiled only, & untested/unverified to work):

Code:
String B_path[10];
String Str_A = "\\AUDIO\\B_";
String Str_B = "\\";

void setup() {
   Serial.begin(9600);
   for (int i = 0; i < 10; i++)  {
      B_path[i] = Str_A + String(i) + Str_B;
      Serial.println(B_path[i]);
   }
}

void loop() {
}

Hope that helps . . .

Mark J Culross
KD5RXT
 
Thank you
Mark J Culross
KD5RXT.
it is working as i want. i will try it to open folder & pic files name and try somthing like below
C++:
String myWav = "SnrRoll.wav";
String myWav_Fullpath = B_path[i] + myWav;
 playSdWav1.playWav(myWav_Fullpath.c_str());
will report if something wrong...
thank you...
 
Last edited:
You might also consider using '/' as your path separators and let microsoft's appalling choice of separator just turn into a distant memory. As a bonus your code will also just work on linux filesystems without any problems.

Edit: I consider MS's choice of '\' as a path separator to be appalling merely because it conflicts with the C compiler's use of the '\' character as an escape character, not because of any inherent fanboi tendencies
 
Last edited:
You might also consider using '/' as your path separators and let microsoft's appalling choice of separator just turn into a distant memory. As a bonus your code will also just work on linux filesystems without any problems.

Edit: I consider MS's choice of '\' as a path separator to be appalling merely because it conflicts with the C compiler's use of the '\' character as an escape character, not because of any inherent fanboi tendencies
Maybe we can blame IBM :LOL:
 
Back
Top