From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Recursively Retrieving All the Files/Directories in a Directory

Hello,

I want to retrieve all files in a directory, and ran into this sample code:

 

www.ni.com/example/27157/en/

 

This is the function that does the retrieve action:  GetFilesAndDirectories().  Its content is as follow:

 

void GetFilesAndDirectories(char dir[], int* numItems, int parentItem)
{
char fileName[MAX_PATHNAME_LEN], searchPath[MAX_PATHNAME_LEN];
int error = 0;
strcpy (searchPath, dir);
strcat (searchPath, "\\*");

if (!GetFirstFile (searchPath, 1, 1, 0, 0, 0, 0, fileName)) // has at least one file
{
InsertListItem (panelHandle, PANEL_LISTBOX, -1, fileName, (*numItems)++);
while (!GetNextFile (fileName))
{
InsertListItem (panelHandle, PANEL_LISTBOX, -1, fileName, (*numItems)++);
}
}

if (!GetFirstFile (searchPath, 0, 0, 0, 0, 0, 1, fileName)) // has at least one directory
{
char dirNamesSaved[MAX_NUM_DIR][MAX_PATHNAME_LEN];
int numDir = 0, i;

strcpy (dirNamesSaved[numDir++], fileName);
while (!GetNextFile (fileName) && numDir<MAX_NUM_DIR) // first save all the directory names
{
strcpy (dirNamesSaved[numDir++], fileName);
}

for (i=0; i<numDir; i++)
{
char displayName[MAX_PATHNAME_LEN];
Fmt (displayName, "%s<%s%s", fileName, " (Directory)");
InsertListItem (panelHandle, PANEL_LISTBOX, -1, displayName, (*numItems)++);
MakePathname (dir, dirNamesSaved[i], searchPath);
GetFilesAndDirectories(searchPath, numItems, (*numItems)-1);
}
}
}

 

I then modify that function into one that I can use to retrieve either files or folders, and populate a ring object with the result.  I call it Ring_Populate().  Its content is as follow:

 

void Ring_Populate (int panel, int TargetRing, char TargetFolder[], int Directory_Bool, int *Iteration_Now, int Iteration_Prev)
{
char FileName [260]; //standard = 260 max char. w/ null char.
char Folder [260];

strcpy (Folder, TargetFolder); //copy string
strcat (Folder, "*"); //add wildcard character, * or ?

switch (Directory_Bool)
{
case 0: //file
if (!GetFirstFile(Folder, 1, 1, 0, 0, 0, 0, FileName)) //has at least 1 file
{
InsertListItem (panel, TargetRing, -1, FileName, (*Iteration_Now)++);

while (!GetNextFile(FileName))
{
InsertListItem (panel, TargetRing, -1, FileName, (*Iteration_Now)++);
}
}
break;

default: //folder
if (!GetFirstFile(Folder, 0, 0, 0, 0, 0, 1, FileName)) //has at least 1 folder
{
int Folder_No = 0;
int Folder_Max = 50; //max 50 folders
char Folder_List [50][260]; //name 260 max

strcpy (Folder_List [Folder_No++], FileName); //copy first element then ++

while ( (!GetNextFile(FileName)) && (Folder_No < Folder_Max) )
{
strcpy (Folder_List [Folder_No++], FileName); //copy all folder names
}

for (int i = 0; i < Folder_No; i++)
{
char modFolder [260];

//Fmt (modFolder, "%s<%s%s", FileName, " (DIR)");
//InsertListItem (panel, TargetRing, -1, modFolder, (*Iteration_Now)++);
//MakePathname (TargetFolder, Folder_List [i], Folder); //recycle
//Ring_Populate (panel, TargetRing, TargetFolder, 1, Iteration_Now, (*Iteration_Now) - 1);

Fmt (modFolder, "%s<%s%s", Folder_List [i], " (DIR)");
InsertListItem (panel, TargetRing, -1, modFolder, i);
}
}
break;
}
}

 

It works fine.  However, the section that I commented out (modified from the sample code, which works) gives me stack overflow.

 

//Fmt (modFolder, "%s<%s%s", FileName, " (DIR)");
//InsertListItem (panel, TargetRing, -1, modFolder, (*Iteration_Now)++);
//MakePathname (TargetFolder, Folder_List [i], Folder); //recycle
//Ring_Populate (panel, TargetRing, TargetFolder, 1, Iteration_Now, (*Iteration_Now) - 1);

 

This is the original code:

 

char displayName[MAX_PATHNAME_LEN];
Fmt (displayName, "%s<%s%s", fileName, " (Directory)");
InsertListItem (panelHandle, PANEL_LISTBOX, -1, displayName, (*numItems)++);
MakePathname (dir, dirNamesSaved[i], searchPath);
GetFilesAndDirectories(searchPath, numItems, (*numItems)-1);

 

Question:

1. How do I get stack overflow?  My code is essentially the same.

2. My modified code works fine retrieving the list of folders.  I do not see the need to call the function recursively.  Can anyone explain?

 

 

 

 

Message 1 of 1
(4,334 Views)