how to delete all the files from sdcard except "mytext.csv"

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to delete all the files from sdcard except "mytext.csv"

Jump to solution
2,450 Views
Swapnil_Enable
Contributor III

hello, i am working with the MFS in MQX with sdcard. the file system will install on the sdcard. open a mytext.csv file and write on it. now the above code is success fully working. i could log all related data in hot plug n play sdcard. Now with my next requirement in it is, i have to delete all the files (temp, *.*) in the sdcard except the "mytext.csv". i am not able to find proper approch or command in mfs library or MQXMFSUG.pdf file. can any one please give me suggestion that how can i achieve this condition. please help in regards, Regards, Swapnil K.

Labels (1)
0 Kudos
Reply
1 Solution
1,602 Views
Martin_
NXP Employee
NXP Employee

List all files and subdirectories in a directory.

MFS_SEARCH_DATA search_data;

MFS_SEARCH_PARAM search;

char filepath = “*.*”;

search.ATTRIBUTE = MFS_SEARCH_ANY;

search.WILDCARD = filepath;

search.SEARCH_DATA_PTR = &search_data;

error_code = ioctl(mfs_fd_ptr, IO_IOCTL_FIND_FIRST_FILE, (uint_32_ptr) &search);

while (error_code == MFS_NO_ERROR)

{

  printf ("%-12.12s %6lu %02lu-%02lu-%04lu %02lu:%02lu:%02lu\n", search_data.NAME, search_data.FILE_SIZE,

     (uint_32)(search_data.DATE & MFS_MASK_MONTH) >>MFS_SHIFT_MONTH,

     (uint_32)(search_data.DATE & MFS_MASK_DAY) >> MFS_SHIFT_DAY,

     (uint_32)((search_data.DATE & MFS_MASK_YEAR) >> MFS_SHIFT_YEAR) + 1980,

     (uint_32)(search_data.TIME & MFS_MASK_HOURS) >> MFS_SHIFT_HOURS,

     (uint_32)(search_data.TIME & MFS_MASK_MINUTES) >> MFS_SHIFT_MINUTES,

     (uint_32)(search_data.TIME & MFS_MASK_SECONDS) << 1);

  error_code = ioctl(mfs_fd_ptr, IO_IOCTL_FIND_NEXT_FILE, (uint_32_ptr) &search_data);

}

View solution in original post

0 Kudos
Reply
8 Replies
1,602 Views
timias
Contributor IV

I don't know if this is possible for you, but you could simply load the file "MyText.csv" into RAM (depending on it's size of course), format the SD Card and then re-write the file. You are going to need to recursively search through all directories, removing all files and sub directories otherwise.

I am pretty sure the recursive method also will not reclaim space of files that might have been left open when power was removed.

0 Kudos
Reply
1,602 Views
firebrizz
Contributor II

I think you should use the ioctl function with IO_IOCTL_FIND_FIRST_FILE,IO_IOCTL_FIND_NEXT_FILE  used as I/O control command for getting list of files, and with IO_IOCTL_DELETE_FILE for deleting files.

0 Kudos
Reply
1,602 Views
Swapnil_Enable
Contributor III

is there any example which i can refer.  a simple *.* search with which i can understand the working of the IO_IOCTL_FIND_FIRST_FILE,IO_IOCTL_FIND_NEXT_FILE . basically my dought is how will i get the path name of all files, which is required by the IO_IOCTL_DELETE_FILE as third parameter in ioclt function

0 Kudos
Reply
1,603 Views
Martin_
NXP Employee
NXP Employee

List all files and subdirectories in a directory.

MFS_SEARCH_DATA search_data;

MFS_SEARCH_PARAM search;

char filepath = “*.*”;

search.ATTRIBUTE = MFS_SEARCH_ANY;

search.WILDCARD = filepath;

search.SEARCH_DATA_PTR = &search_data;

error_code = ioctl(mfs_fd_ptr, IO_IOCTL_FIND_FIRST_FILE, (uint_32_ptr) &search);

while (error_code == MFS_NO_ERROR)

{

  printf ("%-12.12s %6lu %02lu-%02lu-%04lu %02lu:%02lu:%02lu\n", search_data.NAME, search_data.FILE_SIZE,

     (uint_32)(search_data.DATE & MFS_MASK_MONTH) >>MFS_SHIFT_MONTH,

     (uint_32)(search_data.DATE & MFS_MASK_DAY) >> MFS_SHIFT_DAY,

     (uint_32)((search_data.DATE & MFS_MASK_YEAR) >> MFS_SHIFT_YEAR) + 1980,

     (uint_32)(search_data.TIME & MFS_MASK_HOURS) >> MFS_SHIFT_HOURS,

     (uint_32)(search_data.TIME & MFS_MASK_MINUTES) >> MFS_SHIFT_MINUTES,

     (uint_32)(search_data.TIME & MFS_MASK_SECONDS) << 1);

  error_code = ioctl(mfs_fd_ptr, IO_IOCTL_FIND_NEXT_FILE, (uint_32_ptr) &search_data);

}

0 Kudos
Reply
1,602 Views
netra
Contributor IV

Hi,

I am new in mqx Can u plaz tell what is "mfs_fd_ptr"  ??

my device is "ram" and installed mfs is "a:", which shd be open using fopen() before using example present in mqs to search all files

plz reply

0 Kudos
Reply
1,602 Views
Swapnil_Enable
Contributor III

please go through the C:\Program Files\Freescale\Freescale MQX 3.8\mfs\examples\ramdisk example will state you how to install file system on RAM (0x2000) space and use it as a drive. u can then use normal file handling API to create, delete, read, write in a file.

1,602 Views
Swapnil_Enable
Contributor III

thanks Martin with little addition i could make it work for my logic. but the problem i am facing is only files are getting deleted not the folders. please suggest me how can i do that...

0 Kudos
Reply
1,602 Views
firebrizz
Contributor II

you first need to delete all the files within the folder, and then use ioctrl with IO_IOCTL_REMOVE_SUBDIR command

0 Kudos
Reply