Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by pascal-vehigy on January 27, 2016

To whom it may concern,

I am cycling through all files in a given directory, processing them, and afterwards deleting them. Is it possible, that the ff_findnext function does not work if a file in the current working directory is being deleted in between iterations?

My sequence of events is something like that: 1. change working directory with ffchdir 2. allocate some memory and get the pointer for the FFFindDatat struct 3. RetVal = fffindfirst( "", pxFindStruct ) 4. if RetVal != 0 -> abort 5. if it is a dot (. or ..) entry or another directory skip it 6. else open the file ffopen(pxFindStruct->pcFileName, "r"), send it via TCP, finally ffclose(...) 7. if the TCP server on the other end responded positively, delete the file: ffremove( pxFindStruct->pcFileName ) 8. RetVal = fffindnext( pxFindStruct ) 9. loop back to 4 while RetVal == 0

Am I missing something? Does the FindStruct get messed up because I am deleting files while I iterate through them?

If there is only one file in the directory, it works perfectly fine. If a few files have been accumulated, the first one is being sent and removed, but then ff_findnext breaks the loop. However, I can access the very same FAT through FTP and fetch the remainng files. They are perfectly readable, the size and content is exactly as expected.

Thanks for any hints on what might be going on here.

Sincerely, Pascal

+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by rtel on January 27, 2016

Hi Pascal, thank you for your valuable description. We will check it out and report back here.

+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by heinbali01 on January 27, 2016

Hi Pascal,

I'm not sure how W32 or Linux will respond if you try to do the same: delete the current file while iterating through a directory.

Here is a solution, although it will use ffconfigMAX_FILENAME bytes on the stack:

~~~~~ BaseTypet xFindResult = fffindfirst( pcPath, &xFindData ); while( xFindResult >= 0 ) { char pcFileName[ ffconfigMAX_FILENAME ];

    snprintf( pcFileName, sizeof( pcFileName ),
        xFindData.xDirectoryEntry.pcFileName );
    /* Move to the next file before the
    current entry gets deleted. */
    xFindResult = ff_findnext( &xFindData );
    SendFile( pcFileName );
    ff_remove( pcFileName );


PS. If you need to access all properties of "the current file", you would have to make a copy of xFindData.xDirectoryEntry which has the type FF_DirEnt_t.


+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by pascal-vehigy on January 27, 2016

Hi Hein,

thank you for the quick reply. Yep, your way does make more sense, I guess I was too greedy with stack usage. But in my case the files, which obviously are a backlog as you undoubtetly have figured, have a fixed naming scheme, so it is no problem to buffer the "current" filename on the stack.

I haven't actually gotten to try it out yet, but I am sure this approach will work. Otherwise I will be posting back.

Have a nice evening (or whatever time of day it is for you), Pascal

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Meet us at Embedded World. Hall 3A-525.

Hear from Richard Barry at Embedded World. Feb 28, 16:00, Hall 4-428.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS