This example shows how to handle directories using Waspmote SD
File:
"WaspSD_2_handlingDirectories.pde"
/*
* ------Waspmote SD Handling Directories Example--------
*
* Explanation: This example shows how to handle directories using
* Waspmote SD
*
* Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Version: 0.1
* Design: David Gascón
* Implementation: Alberto Bielsa
*/
const char* command="Folder1";
const char* command2="Folder2";
const char* command3="Test4";
const char* filename="file3.txt";
void setup() {
USB.begin();
USB.println("Start WASP FAT 16 SD-Card Test\n");
// Set SD ON
SD.ON();
}
void loop()
{
// Init SD
USB.println("DISK INFO: ");
USB.println(SD.print_disk_info());
// Getting Disk Size
USB.print("Disk Size: ");
USB.println(SD.getDiskSize());
USB.print("Disk Free: ");
USB.println(SD.getDiskFree());
// Listing Root Directory. Initially empty
USB.println("List root Directory: ");
USB.println(SD.ls());
// Creating Files
if(SD.create("Test_File_1")) USB.println("Created a file called 'Test_File_1'");
if(SD.create("Test_File_2")) USB.println("Created a file called 'Test_File_2'");
// Creating Directories
if(SD.mkdir("Folder")) USB.println("Created directory 'Folder'");
if(SD.mkdir("Folder2")) USB.println("Created directory 'Folder2'");
USB.println("List root Directory: ");
USB.println(SD.ls());
// Getting atributes of the directories
if(!SD.isFile("Folder")) USB.println("'Folder' is not a file");
if(SD.isDir("Folder")) USB.println("'Folder' is a dir");
USB.print("Attributes of 'Folder': ");
USB.println(SD.getAttributes("Folder"));
if(SD.cd("Folder")) USB.println("Entering into 'Folder' ");
USB.println("List 'Folder'");
USB.println(SD.ls());
if(SD.cd("..")) USB.println("Getting out of 'Folder' ");
// Moving through the directories
USB.println("List root Directory: ");
USB.println(SD.ls());
if(SD.cd("Folder2")) USB.println("Entering into 'Folder2' ");
if(SD.create("FileText5.txt")) USB.println("'FileText5.txt' created");
USB.println("List 'Folder2' Directory: ");
USB.println(SD.ls());
// Deleting files inside a directory
if(SD.del(".")) USB.println("Content of 'Folder2' deleted");
USB.println("List 'Folder2' Directory: ");
USB.println(SD.ls());
if(SD.cd("..")) USB.println("Getting out of 'Folder2' ");
// Deleting directories
if(SD.del("Folder")) USB.println("'Folder' deleted");
USB.println("List root directory: ");
USB.println(SD.ls());
if(SD.del("Folder2")) USB.println("'Folder2' deleted");
USB.println("List root directory: ");
USB.println(SD.ls());
// Deleting all created files
if(SD.del("Test_File_1")) USB.println("File 'Test_File_1' deleted");
if(SD.del("Test_File_2")) USB.println("File 'Test_File_2' deleted");
// Same disk free as when starting the example
USB.println("List root directory: ");
USB.println(SD.ls());
USB.print("Disk Free: ");
USB.println(SD.getDiskFree());
delay(5000);
}
You can download the code of this example.