File handling

Overview

The file handling API can be used to manage the SD-card content of a device with t-script capablility. It allows listing files, copying files to and from the device, deleting files and formatting the SD-card (FAT32).

Enumerate files

The file system of the SD-card is flat, meaning the files are indexed and stored in sequential order. The functions available for checking the content of the SD-card are kvFileGetCount(), which as the name states retrieves the number of files on the SD-card, and kvFileGetName() which retrieves the filename based on its index. With these the file content of the SD-card can be enumerated.

Example: Enumerate SD-card content.

char buf[64];
stat = kvFileGetCount(hnd, &fileCount);
for (int i=0; i < fileCount; i++){
stat = kvFileGetName(hnd, i, buf, sizeof(buf));
printf("%3d: %s\n", i, buf);
}

Copying files from / to device

Files can be copied to the device using the function kvFileCopyToDevice(). Note that since the SD-card is formated using FAT32, a 8.3 formated filename as the target filename should be used. The device only allows flat file structure so no folders can be passed.

Example: Copying files to device.

// Copy file to device.
// Separator "/" allowed on windows
stat = kvFileCopyToDevice(hnd, "path/to/file1.txt", "file1.txt");

To instead copy files from the device, use kvFileCopyFromDevice().

Example: Copying files from the device.

// Copy file from device.
// Separator "/" allowed on windows.
stat = kvFileCopyFromDevice(hnd, "file1.txt", "path/to/new_file.txt");

Deleting files

To delete files, use kvFileDelete() and pass the name of the file on the SD-card.

Example: Deleting files from device.

// Delete file from SD-card
stat = kvFileDelete(hnd, "file1.txt");

Disk formatting

To reformat the SD-card, use kvFileDiskFormat(). Full initialization can be done with the kvmlib function kvmDeviceFormatDisk(). Example: Formatting the SD-card.

// Format SD-card to FAT32
stat = kvFileDiskFormat(hnd);
See also
kvmDeviceFormatDisk()