/********************************************* *@brief 在文件夹中遍历子文件夹,返回最后文件夹名字指针 *@param [in ] directory_path *@param [out] directory_name *@param [in ] pattern 匹配模式 *@return ********************************************** */ static int traverse_subcatalog(char *directory_path, char *directory_name, char *pattern) { int res; DIR directory_object; FILINFO file_object; #if _USE_LFN char lfn[_MAX_LFN + 1]; file_object.lfname = lfn; file_object.lfsize = _MAX_LFN + 1; #endif res = f_findfirst(&directory_object, &file_object, directory_path, pattern); while (res == FR_OK && file_object.fname[0]) { #if _USE_LFN DBG_print(DBG_DEBUG, "%-12s %s", file_object.fname, file_object.lfname); #else DBG_print(DBG_DEBUG, "%s", (file_object.fname)); #endif strcpy(directory_name, file_object.fname); //TODO: 要优化效率 res = f_findnext(&directory_object, &file_object); } // if (0 != file_object.fname[0]) // { // strcpy(directory_name, file_object.fname); //directory_name = file_object.fname; // } f_closedir(&directory_object); return 0; }
/********************************************* *@brief 在文件夹中遍历子文件,记录文件大小信息 *@param [in ] directory_path *@param [out] directory_name *@param [in ] pattern 匹配模式,比如“*.fsn” *@return ********************************************** */ static int traverse_subfile(char *file_path, sd_file_manage_t *p_sd_file_manage, char *pattern) { int res; DIR directory_object; FILINFO file_object; #if _USE_LFN char lfn[_MAX_LFN + 1]; file_object.lfname = lfn; file_object.lfsize = _MAX_LFN + 1; #endif res = f_findfirst(&directory_object, &file_object, file_path, pattern); while (res == FR_OK && file_object.fname[0]) { #if _USE_LFN DBG_print(DBG_DEBUG, "%-12s %s", file_object.fname, file_object.lfname); #else DBG_print(DBG_DEBUG, "%s", (file_object.fname)); p_sd_file_manage->unload_file_num++; p_sd_file_manage->unload_file_size += file_object.fsize; #endif res = f_findnext(&directory_object, &file_object); } f_closedir(&directory_object); return 0; }
directory_iterator& directory_iterator::operator++() { const auto result = f_findnext(&impl->dir, &impl->filinfo); if( (result != FR_OK) || (impl->filinfo.fname[0] == 0) ) { impl.reset(); } return *this; }
/** * Initialize the peripherals and state for this task. */ uint8_t camera_task_setup() { /* Initialize I2C peripherals for this task. */ ov5642_init(); lps331_init(); hts221_init(); /* Intialize SPI peripherals for this task. */ spi_take(); arduCamInstalled = arducam_init(); /* Try to mount the SD card. */ if (fn_initvolume(mmc_spi_initfunc) != F_NO_ERROR) { trace_printf("sdcard: failed to mount volume\n"); spi_give(); return 0; } #ifdef CLEAN_SD_CARD /* Delete DATA.LOG and JPG files from the SD card. */ trace_printf("camera_task: cleaning SD card\n"); f_delete("DATA.LOG"); f_delete("file.txt"); F_FIND xFindStruct; if (f_findfirst("*.JPG", &xFindStruct) == F_NO_ERROR) { do { f_delete(xFindStruct.filename); } while (f_findnext(&xFindStruct) == F_NO_ERROR); } #endif spi_give(); return 1; // OK }
void ListDir() { F_FIND finder; // location to store the information retreived /* Find first file or subdirectory in specified directory. First call the f_findfirst function, and if file was found get the next file with f_findnext function. Files with the system attribute set will be ignored. Note: If f_findfirst() is called with "*.*" and the current directory is not the root directory, the first entry found will be "." - the current directory. */ volatile int rc = f_findfirst( "*.*", &finder ); if ( rc == F_NO_ERROR ) // found a file or directory { f_getcwd( currDirBuf, CWD_BUFFER_SIZE ); iprintf("\r\n"); iprintf("Current Directory: %s\r\n", currDirBuf); do { if ( ( finder.attr & F_ATTR_DIR ) ) { iprintf( "[%8s/] d\r\n", finder.filename ); } else { iprintf( "%12.12s f %d\r\n", finder.filename, finder.filesize ); } } while ( !f_findnext( &finder ) ); iprintf("\r\n"); } else { iprintf("\r\nError in listing directory\r\n"); } }
size_t enumerateDir(char ***output, char *path, char *pattern) { DIR dp; FILINFO fno; FRESULT fr; char **out = NULL; size_t pathlen = strlen(path); if (pathlen >= FF_LFN_BUF) goto end; fr = f_findfirst(&dp, &fno, path, pattern); char pathb[FF_LFN_BUF] = {0}; strcpy(pathb, path); pathb[pathlen] = '/'; int i = 0; while (fno.fname[0] != 0 && fr == FR_OK) { if (fno.fname[0] == '.') goto next; out = (char **)realloc(out, (i+1) * sizeof(char *)); out[i] = (char *)malloc(FF_LFN_BUF); strcpy(out[i], pathb); strcat(out[i], fno.fname); pathb[pathlen+1] = 0; i++; next: f_findnext(&dp, &fno); } end: f_closedir(&dp); *output = out; return i; }
size_t FileMaxSize(const wchar_t *path, const wchar_t *pattern) { size_t maxSize = 0; DIR dir; wchar_t lfn[_MAX_LFN + 1]; FILINFO fno; fno.lfname = lfn; fno.lfsize = _MAX_LFN + 1; if (f_findfirst(&dir, &fno, path, pattern) == FR_OK) { do { if (fno.fsize > maxSize) maxSize = fno.fsize; } while (f_findnext(&dir, &fno) == FR_OK && fno.fname[0]); } f_closedir(&dir); return maxSize; }
/** * @brief List up to 25 file on the root directory with extension .BMP * @param DirName: Directory name * @param Files: Buffer to contain read files * @retval The number of the found files */ uint32_t Storage_GetDirectoryBitmapFiles(const char* DirName, char* Files[]) { FRESULT res; uint32_t index = 0; /* Open filesystem */ if(f_mount(&fs, (TCHAR const*)"",0) != FR_OK) { return 0; } /* Start to search for wave files */ res = f_findfirst(&MyDirectory, &MyFileInfo, DirName, "*.bmp"); /* Repeat while an item is found */ while (MyFileInfo.fname[0]) { if(res == FR_OK) { if(index < MAX_BMP_FILES) { sprintf (Files[index++], "%s", MyFileInfo.fname); } /* Search for next item */ res = f_findnext(&MyDirectory, &MyFileInfo); } else { index = 0; break; } } f_closedir(&MyDirectory); return index; }
uint32_t tmdPreloadRecent(tmd_data *data, wchar_t *path) { //returns tmd file name contentid DIR dir; FILINFO fno; fno.lfname = NULL; wchar_t *filename; tmd_data data_tmp; uint32_t contentid = 0xFFFFFFFF; if (f_findfirst(&dir, &fno, path, L"*.tmd") == FR_OK) { filename = path + wcslen(wcscat(path, L"/")); do { wcscat(filename, fno.fname); if (tmdPreloadHeader(&data_tmp, path) && (contentid == 0xFFFFFFFF || __builtin_bswap16(data_tmp.header.title_version) > __builtin_bswap16(data->header.title_version)) ) { *data = data_tmp; contentid = wcstoul(filename, NULL, 16); } } while (f_findnext(&dir, &fno) == FR_OK && *fno.fname); } f_closedir(&dir); return contentid; }
static portBASE_TYPE prvDIRCommand( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ) { static F_FIND *pxFindStruct = NULL; unsigned char ucReturned; portBASE_TYPE xReturn = pdFALSE; /* This assumes pcWriteBuffer is long enough. */ ( void ) pcCommandString; /* Ensure the buffer leaves space for the \r\n. */ configASSERT( xWriteBufferLen > ( strlen( cliNEW_LINE ) * 2 ) ); xWriteBufferLen -= strlen( cliNEW_LINE ); if( pxFindStruct == NULL ) { /* This is the first time this function has been executed since the Dir command was run. Create the find structure. */ pxFindStruct = ( F_FIND * ) pvPortMalloc( sizeof( F_FIND ) ); if( pxFindStruct != NULL ) { ucReturned = f_findfirst( "*.*", pxFindStruct ); if( ucReturned == F_NO_ERROR ) { prvCreateFileInfoString( pcWriteBuffer, pxFindStruct ); xReturn = pdPASS; } else { snprintf( pcWriteBuffer, xWriteBufferLen, "Error: f_findfirst() failed." ); } } else { snprintf( pcWriteBuffer, xWriteBufferLen, "Failed to allocate RAM (using heap_4.c will prevent fragmentation)." ); } } else { /* The find struct has already been created. Find the next file in the directory. */ ucReturned = f_findnext( pxFindStruct ); if( ucReturned == F_NO_ERROR ) { prvCreateFileInfoString( pcWriteBuffer, pxFindStruct ); xReturn = pdPASS; } else { /* There are no more files. Free the find structure. */ vPortFree( pxFindStruct ); pxFindStruct = NULL; /* No string to return. */ pcWriteBuffer[ 0 ] = 0x00; } } strcat( pcWriteBuffer, cliNEW_LINE ); return xReturn; }