LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Directory file listing

I am trying to get a file listing from a directory. I want to filter the file extensions and load the filtered filenames into an array.
( I know about the "GetFirstFile() and GetNextFile()" functions) does any
body know an elegant way of doing it?
0 Kudos
Message 1 of 2
(3,079 Views)
This self-explanatory code section provides an example on how to use the file listing functions.

/****************************************************************************/
/* GetDirectFiles */
/****************************************************************************/

/* TASK: this function gets the number of archives in the given folder */
/* PARAMETERS: dirname -> the name of the directory */
/* RETURNS: -1 if the directory does not exist, the number of archives otherwise */
/* NOTES: none */

int GetDirectFiles (char dirname [])
{
char lname [MAX_PATHNAME_LEN];
char lname1 [MAX_PATHNAME_LEN];
int result = -1;

/* If the directory exists */
if (GetFirstFile (dirname, 0, 0, 0, 0, 0, 1, lname)
== 0)
{
result = 0;
strcpy (lname, dirname);
strcat (lname, "\\*.*");

/* Get the first file in the directory */
if (GetFirstFile (lname, 1, 0, 0, 0, 0, 0, lname1) == 0)
{
result++;

/* Get the next file in the directory */
while (GetNextFile (lname1) == 0)
result++;
}
}

return result;
}

You may enhance it by supplying the required file extension as a parameter (instead of *.*).
You should also create your own list of file names to store the results from GetFirstFile and GetNextFile.

Bye
Giovanni
Message 2 of 2
(3,079 Views)