Directory operations
Last updated
Was this helpful?
Last updated
Was this helpful?
To organize an SD card, it is possible to create and manage directories. There are some functions related with directories.
The mkdir()
function creates a directory given as a valid directory path (according to short filename format) in the current working directory. The root directory is the default directory each time SD card is initialized.
It returns ‘1’ on creation and ‘0’ on error, activating the flag too.
If a directory name already exists, it will occur an error and the flag will be activated.
Example of use
Creating and deleting directories example:
Empty directories
The rmdir()
function deletes the empty directory specified as input. The directory file will be removed only if it is empty and is not the root directory.
It returns ‘1’ if the directory has been erased properly and ‘0’ if error.
Example of use
Non-empty directories
The rmRfDir()
function deletes the non-empty directory specified as input and all contained files. It returns ‘1’ if the directory has been erased properly and ‘0’ if error.
Example of use
The ls()
function prints through the USB port the contents of the current working directory. It is possible to introduce three different flags which may be an inclusive OR of:
LS_DATE
- Print file modification date
LS_SIZE
- Print file size.
LS_R
- Recursive list of subdirectories.
It returns nothing. The information is printed through the USB port.
Example of use
An example of the output for SD.ls(LS_R|LS_DATE|LS_SIZE)
; would be:
FILE8 1980-01-01 00:00:00 204
FILE2 1980-01-01 00:00:00 2754
FOLDER/ 2000-01-01 01:00:00
SUBFOLD/ 2000-01-01 01:00:00
FILE.TXT 2012-06-11 11:58:10 811
The isDir()
function finds a sub-directory in the current directory. If it exists and it is a directory ‘1’ will be returned, ‘0’ will be returned if it exists but it is not a directory and ‘-1’ will be returned if it does not exist.
Example of use
The numFiles()
function gets the amount of files and subdirectories in the current directory. It returns the number of files or directories found, or zero if there are no files or directories. It does not count ‘.’ and ‘..’ directories, so if there are no directories or files in the current directory, zero will be returned.
If an error occurs, a negative number is returned.
Example of use
The cd()
functionchanges the current working directory pointer to the directory given as a parameter. It returns ‘0’ if error, and ‘1’ if not.
Example of use
Go directly to root directory
The goRoot()
function permits to go to root directory directly. It returns '1' when ok, '0' when error.
Example of use
Creating and deleting directories example:
Listing directories example: Creating and deleting directories example:
Change current working directory example: