The FS-Manager is a high-level library that provides an easy way to manage files and directories.
The FS-Manager is a high-level file system interface designed specifically for Arduino-based projects. It provides an easy-to-use and efficient way to manage files and directories on devices that support LittleFS or SPIFFS (Serial Peripheral Interface Flash File System).
With this library, you can perform various file operations such as creating, opening, reading, writing, and deleting files. It abstracts the low-level complexities of interacting with the underlying file system, allowing you to focus on implementing your project's logic without worrying about the intricacies of file management.
This library is specifically designed and tested for use with LittleFS. Due to the deprecation of SPIFFS and its potential removal in future releases, we strongly recommend using LittleFS as the underlying file system with this library. Using SPIFFS instead may result in compatibility issues and bugs. Therefore, it is highly advised to utilize LittleFS to ensure optimal performance and stability.
- Download the .zip file from github tag section.
- In the Arduino IDE, open the "Sketch" menu.
- Click on "Include Library".
- Select "Add .ZIP Library..." from the drop-down menu that appears.
Add https://github.com/hva23/FS-Manager.git to lib_deps
tag in platformio.ini
file.
Or add hva23/FS-Manager@^(desired_version)
to to lib_deps
tag in platformio.ini
file.
FileManager fileManager(&LittleFS);
// or FileManager fileManager(&SPIFFS);
bool begin();
directory
-Where file will be created
fileName
-File name
contents
-Init content to write to file, could be null
/* Create */
FileStatus::Value create(String directory, String fileName, String contents);
directory
-Where file will be read
fileName
-File name
contents
-The contents of the file will be stored in it
/* Read */
FileStatus::Value read(String directory, String fileName, String &contents);
directory
-Where file will be updated
fileName
-File name
contents
-The contents to replace
/* Update */
FileStatus::Value write(String directory, String fileName, String contents);
directory
-Where file will be updated
fileName
-File name
contents
-An array of uint8_t
data type to write and replace the previous contents
fileSize
-Size of uint8_t
array
/* Update */
FileStatus::Value write(String directory, String fileName, uint8_t *contents, size_t fileSize);
directory
-Where file will be updated
fileName
-File name
contents
-The contents to add to the previous contents
/* Update */
FileStatus::Value append(String directory, String fileName, String contents);
directory
-Where file will be updated
fileName
-File name
contents
-An array of uint8_t
data type to write and add to the previous contents
fileSize
-Size of uint8_t
array
/* Update */
FileStatus::Value append(String directory, String fileName, uint8_t *contents, size_t fileSize);
directory
-Where file will be deleted
fileName
-File name
/* Delete */
FileStatus::Value deleteFile(String directory, String fileName);
CRUD Example
const String dir = "Directory1/Subdirectory1/";
const String fileName = "file.txt";
FileManager fileManager(&LittleFS);
FileStatus::Value fileStatus;
/* LittleFS or SPIFFS */
fileManager.begin();
fileStatus = fileManager.create(dir, fileName);
if (fileStatus != FileStatus::Success)
Serial.println("File creation failed");
else
{
String contentsToWrite = "Hello world!";
fileStatus = fileManager.write(dir, fileName, contentsToWrite);
if(fileStatus == FileStatus::Failed)
Serial.println("File write failed");
else
{
String fileContents = "";
fileStatus = fileManager.read(dir, fileName, fileContents);
if(fileStatus == FileStatus::Success)
{
Serial.println("File reading was successful!");
Serial.print("Contents : ");
Serial.println(fileContents);
}
}
}