File System Component  Version 5.0
MDK-Professional Middleware for Devices with Flash File System
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
File Maintenance Routines

File maintenance routines perform file management operations. More...

Functions

fsStatus fdelete (const char *path)
 Delete a file or directory with given path name.
 
fsStatus ffind (const char *pattern, fsFileInfo *info)
 Find a file or directory matching search pattern.
 
fsStatus frename (const char *path, const char *newname)
 Rename a file or directory with given path name to a new name.
 
fsStatus fattrib (const char *path, const char *attr)
 Change file attributes.
 

Description

The routines are thread safe.

Function Documentation

fsStatus fattrib ( const char *  path,
const char *  attr 
)
Parameters
[in]pathstring specifying file or directory path.
[in]attrstring specifying file or directory attributes to be modified. The following characters are allowed within par string:
  • + Sets an attribute.
  • - Clears an attribute.
  • R Read-only file attribute.
  • A Archive file attribute.
  • S System file attribute.
  • H Hidden file attribute.
Returns
execution status fsStatus
  • fsOK = Operation successful.
  • fsInvalidParameter = Input parameters are not valid.
  • fsInvalidDrive = Nonexistent drive letter specified.
  • fsInvalidPath = Invalid path was specified.
  • fsTooManyOpenFiles = File cannot be opened due to too many opened files.
  • fsAccessDenied = File attributes change is not allowed.
Note
This function supports FAT drives only.

The function fattrib changes file attributes.

The argument path is a pointer to the string specifying file path and can contain:

  • A file name.
  • A drive prefix. If the drive prefix is omitted, the Default System drive is used.
  • A file path. Use the escape character "\" when specifying a path.
  • A combination of the above.

The argument attr is specifying file attributes to be modified. The following characters are allowed within par string:

CharacterFunction
+ Sets an attribute
- Clears an attribute
R Read-only file attribute
A Archive file attribute
S System file attribute
H Hidden file attribute

Code Example

void cmd_attrib (void) {
char *arg, *path;
// Set parameter
arg = "+R -S";
path = "M0:\\Abstract.txt";
if (fattrib (arg, path) != fsOK) {
printf ("Failed to change file attributes.\n");
}
else {
printf ("File attributes changed.\n");
}
}
fsStatus fdelete ( const char *  path)
Parameters
[in]patha string specifying the file or directory to be deleted.
Returns
execution status fsStatus
  • fsOK = Operation successful.
  • fsInvalidDrive = Nonexistent drive letter specified.
  • fsAccessDenied = File is in use.
  • fsTooManyOpenFiles = File cannot be opened due to too many opened files.
  • fsError = File or folder does not exists or folder not empty.

The function fdelete removes a file or folder from the File System.

The argument path is specifying the file or folder to be deleted and can contain:

  • A file name.
  • A drive prefix. If the drive prefix is omitted, the Default System drive is used.
  • A file path. Use the escape character "\" when specifying a path. To delete a folder, terminate the parameter with a backslash.
  • A combination of the above.

Code Example

void tst_fdelete (void) {
if (fdelete ("TEST.TXT") == fsOK) { // Delete a file from default drive.
printf ("Deleted: TEST.TXT\n");
}
if (fdelete ("R:DATA.LOG") == fsOK) { // Delete a file from RAM drive.
printf ("Deleted: DATA.LOG\n");
}
if (fdelete ("M:\\Work dir\\Temp log.txt") == fsOK) { // Delete a file from sub-folder.
printf ("Deleted: Temporary log.txt\n");
}
if (fdelete "M1:\\Working folder\\") == fsOK) { // Delete a folder, if empty.
printf ("Deleted: Working folder.\n");
}
}
fsStatus ffind ( const char *  pattern,
fsFileInfo info 
)
Parameters
[in]patternstring specifying the pattern.
  • May include drive prefix and the following wildcards:
  • "*" or "*.*" searches for all files in the directory.
  • "abc*" searches for files that begin with abc.
  • "*.htm" searches for files that end with .htm.
  • "abc*.text" searches for files that begin with abc and that end with .text.
[out]infostructure storing information about matching files.
Returns
execution status fsStatus
  • fsOK = Operation successful.
  • fsInvalidDrive = Nonexistent drive letter specified.
  • fsTooManyOpenFiles = File cannot be opened due to too many opened files.
  • fsError = File or folder containing specified pattern cannot be found.

The function ffind searches for files that match specific patterns. The argument pattern is specifying the searching rule and can include:

  • The wildcard *.
    Pattern Description
    "*" or "*.*"Searches for all files in the directory
    "abc*" Searches for files that begin with abc
    "*.htm" Searches for files that end with .htm
    "abc*.text" Searches for files that begin with abc and that end with .text

Code Example

void file_directory (void) {
fsFileInfo info;
info.fileID = 0; // info.fileID must be set to 0
while (ffind ("R:*.*", &info) == fsOK) { // find whatever is in drive "R0:"
printf ("\n%-32s %5d bytes, ID: %04d",
info.name,
info.size,
info.fileID);
}
if (info.fileID == 0) {
printf ("\nNo files...");
}
}
fsStatus frename ( const char *  path,
const char *  newname 
)
Parameters
[in]pathstring specifying the file or directory path.
[in]newnamestring specifying new file or directory name.
Returns
execution status fsStatus
  • fsOK = Operation successful.
  • fsInvalidPath = Invalid path specified.
  • fsInvalidDrive = Nonexistent drive letter specified.
  • fsAccessDenied = File is in use.
  • fsTooManyOpenFiles = File cannot be opened due to too many opened files.
  • fsError = Old file cannot be found or new file already exists.

The function frename replaces the name of a file.

The argument path is specifying the current name of the file. The argument path can contain a drive prefix. In addition, you may also use "" (Default System Drive). For FAT media, path information may also be added.

The argument newname is specifying the new name of the file.

Code Example

void tst_frename (void) {
if (frename ("F:Test.txt", "New name.txt") == fsOK) {
printf ("Rename Successful.\n");
}
}