Pages: [1]   Go Down
Author Topic: How to determine current working directory of the SD card  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

this may be really simple but I just cannot wrap my head around it, and also searching for solutions on the web did not yield any results.

I use the SdFat library and, at one point in my program, I would like to determine what is the current working directory and be able to compare that with e.g. a char array.
Something like:
Code:
Pseudocode:
if (workingDir == "tmp/") { do something }

I know that the vwd() function of SdFat returns a pointer to the working directory, but I fail to do anything with that information.

Thanks a lot in advance.
dodgerts
« Last Edit: April 09, 2013, 10:31:09 am by dodgerts » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read up on the strcmp() function.

-br
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for the quick reply, but that is exactly my problem. The vwd() function returns a SdBaseFile*, and I just do not get how to translate this into a char array to for the comparison...
« Last Edit: April 09, 2013, 08:00:26 am by dodgerts » Logged

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

Quote
The vwd() function returns a SdBaseFile*
The SdBaseFile class has a dirName() function to get the name of the directory. Have you tried that?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The SdBaseFile class has a dirName() function to get the name of the directory. Have you tried that?
Mh, I do not really understand how. The syntax for dirName is:

Code:
void SdBaseFile::dirName ( const dir_t &   dir, char * name )

How do I get the current working directory as dir_t & dir?
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The function SdBaseFile::getFilename() returns file's name.

This example will get the name of the current working directory and print it.

Code:
  char name[13];
  sd.vwd()->getFilename(name);
  Serial.println(name);

Here is documentation for getFilename().
Quote
bool SdBaseFile::getFilename    (    char *     name   )    

Get a file's name

Parameters:
    [out]   name   An array of 13 characters for the file's name.

Returns:
    The value one, true, is returned for success and the value zero, false, is returned for failure

Actually vwd() is a volume's working directory.  Most Arduino users have only one volume so this is also the current working directory.

If you have multiple volumes you should use this code to get the current working directory.
Code:
  SdBaseFile::cwd()->getFilename(name);
  Serial.println(name);
  Serial.println("Done");

Here is more info from the SdFat file WorkingDirectory.txt
Quote
Relative paths in SdFat are resolved in a manner similar to Windows.

Each instance of SdFat has a current directory.  In SdFat this directory
is called the volume working directory, vwd.  Initially this directory is
the root directory for the volume.

The volume working directory is changed by calling SdFat::chdir(path).

The call sd.chdir("/2011") will change the volume working directory
for sd to "/2011", assuming "/2011" exists.

Relative paths for SdFat member functions are resolved by starting at
the volume working directory.

For example, the call sd.mkdir("APRIL") will create the directory
"/2011/APRIL" assuming the volume working directory is "/2011".

SdFat has a current working directory, cwd, that is used to resolve paths
for file.open() calls.

For a single SD card the current working directory is always the volume
working directory for that card.

For multiple SD cards the current working directory is set to the volume
working directory of a card by calling the SdFat::chvol() member function.
The chvol() call is like the Windows <drive letter>: command.

The call sd2.chvol() will set the current working directory to the volume
working directory for sd2.

If the volume working directory for sd2 is "/MUSIC" the call

file.open("BIGBAND.WAV", O_READ);

will then open "/MUSIC/BIGBAND.WAV" on sd2.

The following functions are used to change or get current directories.
See the html documentation for more information.

bool SdFat::chdir(bool set_cwd = false);
bool SdFat::chdir(const char* path, bool set_cwd = false);
void SdFat::chvol();
SdBaseFile* SdFat::vwd();
static SdBaseFile* SdBaseFile::cwd();

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The function SdBaseFile::getFilename() returns file's name.

This example will get the name of the current working directory and print it.

Code:
  char name[13];
  sd.vwd()->getFilename(name);
  Serial.println(name);

Thanks! Works like a charm.

I had read the WorkingDirectory.txt already. However, I am fairly new to C/C++ and did not know I can call functions like you do with sd.vwd()->getFilename().
I learned something conceptually new for me. Thanks again!
Logged

Pages: [1]   Go Up
Jump to: