Hi!
Preface:
i have been reading a bunch, and finally excited to having some vague understanding of how to use a few basic sdfat functions. I think as a newb I had some trouble with a bit of the "::" and "->" and other more experienced c++ nomenclature. Yeah, I don't mean to blame the author, obviously the user should know how to read, and it's a learning opportunity for me.
Main point
I'm trying to make a function where I search the root of my sdcard, and check for directories. I want to name the directories, and eventually check each directory to see if it has a "cfg.txt" in it. If it does, then it's a "username" directory, and that'll be a location to log files in.
A relevant thread seems: http://forum.arduino.cc/index.php/topic,226037.0.html, but it operates on the assumption that the directory name past root is known.
Where I'm at
I think I have, at least, 'getfilename' and 'dirname' functions sort of working. I modified the "openNext" example sketch, so I do properly cycle through all of the directories and files in the root.
However, using getfilename and dirname just return '/' for me, since my working directory is the root, I think. But I don't know how to get into the directories that I find, so as to run dirname and find their name. I can use chdir only if I know what the directory name is, but I don't always know that. I could include a file in the root with "users.csv" or something, but that seems like a crude solution. This function should only be run once per startup, so it doesn't need to be fast.
Any help or tips would be greatly appreciated. I think whatever I'm missing
Code below: I pulled out a few debug serial.print()s, and a couple small things for clarity.
void findDirectories(){
//Start in root.
// Change current directory to root.
if (!sd.chdir()) Serial.println(F("chdir to root failed."));
uint8_t entryLimit = 21;
char UserSet[entryLimit][14];
uint8_t i=0;
// open next file in root. The volume working directory, vwd, is root
while (myFile.openNext(sd.vwd(), O_READ)) {
if (myFile.isDir()) {
// Indicate a directory.
char name[13];
// name for current working directory
myFile.cwd()->getFilename(name);
Serial.print(F("Name is:/"));
Serial.println(name);
Serial.print(F("Or...: "));
dir_t dir;
myFile.dirName(dir, name);
Serial.println(name);
//Neither dirName nor getFileName give me anything other than "/" when these folders have real names.
strlcpy(UserSet[i],name,sizeof(UserSet[i]));
//Later, check that each directory has a cfg.txt before deciding it's part of the UserSet
i = i++;
}
else{ //Other files, which are not directories, remove eventually
}
myFile.close();
}
Serial.println(F("Done with FindDirectories!"));
Serial.println(F("printing list: "));
//Seems I can't just print a 2d char array, so I print it piecewise.
for(i = 0;i<entryLimit;i++){
Serial.println(UserSet[i]);}
}
I should add, that my sd card is basically this:
"1.csv, 2.csv, 3.csv, 5.txt, folder1,folder2,folder3,folder5" and within 3 of the 4 folders I have a cfg.txt file, but nothing else.