Creating, Deleting, and Renaming Files

Discussed in this topic: fd.create, fd.rename, fd.delete, fd.getnumfiles, fd.maxstoredfiles.


You can't really do anything useful with the flash disk unless you create at least one file. The fd.create method is used for this. The string you supply as an argument must include a file name and may also contain the attributes. Some examples:

Tibbo BASIC
If fd.create("File1.dat R 25-JUL-2008")<>PL_FD_STATUS_OK Then ' <File1.dat> is the file name, <R 25-JUL-2008> -- attributes.
   'some problem
End If
If fd.create("    File2")<>PL_FD_STATUS_OK Then 'no attributes defined for this file. Notice leading spaces -- they will be removed.
   'some problem
End If
If fd.create("Database/users.dat")<>PL_FD_STATUS_OK Then 'and here we emulate a directory
   'some problem
End If
If fd.create("file 3")<>PL_FD_STATUS_OK Then 'if the idea was to create a <file 3> file, then this won't work! "3" will be interpreted as attributes!
   'some problem
End If

Naturally, each file on the flash disk must have a unique name, or the 5 — PL_FD_STATUS_DUPLICATE_NAME status code will be generated. Every existing file always has at least one data sector allocated to it. This is how the 7 — PL_FD_STATUS_DATA_FULL error may be generated when you are creating a new file. Finally, the total number of files stored on the flash disk is limited to what you defined when formatting your disk. This maximum can be checked through the fd.maxstoredfiles read-only property. Try to exceed this number and you will get the 6 — PL_FD_STATUS_FILE_TABLE_FULL error code. The current file count can be obtained through fd.getnumfiles.

To delete a file, use the fd.delete method:

Tibbo BASIC
If fd.delete("File1.dat")<>PL_FD_STATUS_OK Then
   'some problem
End If
If fd.delete("File2 abc")<>PL_FD_STATUS_OK Then 'the "abc" part will be ignored -- everything after the space is NOT a part of the file name
   'some problem
End If
If fd.delete("   Database/users.dat")<>PL_FD_STATUS_OK Then 'leading spaces will be ignored
   'some problem
End If

The file you are deleting must, of course, still exist, or you will get the 9 — PL_FD_STATUS_NOT_FOUND status code. You can also rename a file using the fd.rename method (this will preserve file attributes). It is OK to delete or rename a file which is currently open.


The fd.create, fd.rename, and fd.delete methods make changes to the sectors of the flash disk. For the highest possible reliability, use disk transactions when invoking these methods.