p. Read Directory - FReadDir()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

p. Read Directory - FReadDir()

The FReadDir() command is used to read the data from a directory.

 

 

Command Prototype

 

int16_t FReadDir( int16_t Handle, uint8_t* Buffer, int16_t buflen)

 

 

Parameters

Description

Handle

The value returned from FOpenDir()

Buffer

Buffer to receive dir info data

buflen

Length of the buffer

 

Code

 

 

void setup() {

 int16_t handle;

 file_info_t obj_info; //to store information of items belong to a directory

/* Initialize CleO - needs to be done only once */

 CleO.begin();

 

/* Start building a screen frame */

 CleO.Start();

 

 CleO.StringExt(FONT_MEDIUM, 400, 50, LIGHT_GREEN, TM, 0, 0,

 "Creating TestDir...");

/* Create directory TestDir in eFlash */

 CleO.FMKDir("@TestDir");

/* Confirm if the directory is created successfully */

 if(CleO.FStat("@TestDir", NULL) == 1)

 {

   CleO.StringExt(FONT_MEDIUM, 400, 90, LIGHT_GREEN, TM, 0, 0,

   "Create OK");

 }

 else

 {

   CleO.StringExt(FONT_MEDIUM, 400, 90, LIGHT_GREEN, TM, 0, 0,

   "Create FAILED");  

 }

 CleO.StringExt(FONT_MEDIUM, 400, 130, LIGHT_GREEN, TM, 0, 0,

 "Read TestDir...");

/* Create some files and sub directories inside the parent/top directory */

 handle = CleO.FOpen("@TestDir/testfile_1.txt", FILE_CREATE_ALWAYS | FILE_WRITE);

 CleO.FClose(handle);

 handle = CleO.FOpen("@TestDir/testfile_2.txt", FILE_CREATE_ALWAYS | FILE_WRITE);

 CleO.FClose(handle);

 CleO.FMKDir("@TestDir/subdir_1");

 CleO.FMKDir("@TestDir/subdir_2");

/* Open the top directory to read */

  uint8_t buf[30];

  handle = CleO.FOpenDir("@TestDir");

  for(int i = 0; i < 4; i++){

    CleO.FReadDir(handle, (uint8_t *)&obj_info, sizeof(file_info_t));

    if(obj_info.type == CLEO_FILE){

      sprintf((char *)buf, "File: %s", obj_info.filename);

    }

    else {

      sprintf((char *)buf, "Subdir: %s", obj_info.filename);

    }

    CleO.StringExt(FONT_MEDIUM, 400, 170 + 40*i, LIGHT_GREEN, TM, 0, 0, (char *)buf);

  }

/*Close directory handle*/

 CleO.FCloseDir(handle);

/* Delete the top directory */

 CleO.FUnLink("@TestDir");

/* Display completed screen frame */

 CleO.Show();

}

 

void loop() {}

 

 

Description

 

The above code snippet opens the TestDir. The code highlighted in yellow color shows how the FReadDir() command reads the file information from the directory and sub directory. The @ symbol indicates that the directory is opened and read from the eFlash.

 

 

Output

 

Here is the output displayed upon executing the code snippet -

 

FReadDir