The following function recursively walks through a directory (or folder) and lists all files within that directory and all of its subdirectories.
As you can see it is a console function; however, it can be modified for the windows environment by changing the perror() and printf() statements, as required, to fill in a form.
This function is designed to work with Unicode file names. Since I don't read or write
any unicode languages, I guess you will need to tell me if it works! sam@gilcrist.com
Click this link to download the c file.
Return to www.gilcrist.com
/**************************************************************************************
* author : R. Sam Gilcrist
* date : 07 Aug 2003
* purpose : provided a directory name, output to the printer all file names in that
* : directory and subdirectories
**************************************************************************************/
int ListAll(char * szRoot){
TCHAR szFullPath[MAX_PATH + 1];
TCHAR szNewPath[MAX_PATH + 1];
HANDLE fFile;
WIN32_FIND_DATA fileInfo;
TCHAR * pdest;
TCHAR szMsg[MAX_PATH + 128];
int bCont;
strcpy(szFullPath,szRoot);
/*
simple error trap. if the file does not have a vaild handle
print an error to the screen and exit.
*/
if((fFile = FindFirstFile(szRoot,& fileInfo))==INVALID_HANDLE_VALUE){
strcpy(szMsg,"could not find file ");
strcat(szMsg,szRoot);
perror(szMsg);
exit(0);
}else{
/* since we have a vaild file handle, loop thru until FindNextFile return false */
while(fFile && bCont){
/* skip parent (.) and self references (..) */
if((_tcscmp(fileInfo.cFileName,".")!=0)&& (_tcscmp(fileInfo.cFileName,"..")!=0)){
_tcscpy(szFullPath,szRoot);
/* if an * is appended to the path, drop it, add the new file to the path
then add another * at the end */
pdest = _tcschr(szFullPath, '*');
if(pdest !=NULL){
int offset = pdest - szFullPath;
_tcsncpy(szFullPath + offset, fileInfo.cFileName, _tcslen(fileInfo.cFileName)+1 );
}
/* dump data to the screen */
printf("%s\n",szFullPath);
}
/*
if this is a directory and it is not the parent or self pointers
then add a * to the end of the file name so that FindNextFile know to list
all files
*/
if (fileInfo.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY){
if((strcmp(fileInfo.cFileName,".")!=0) && (strcmp(fileInfo.cFileName,"..")!=0)){
_tcscpy(szNewPath,szRoot);
pdest = _tcschr(szNewPath, '*');
if(pdest !=NULL){
int offset = pdest - szNewPath;
_tcsncpy(szNewPath + offset, fileInfo.cFileName, _tcslen(fileInfo.cFileName)+1 );
}
_tcscat(szNewPath,"\\*");
/* recurse up the tree */
ListAll(szNewPath);
}
}
/* get the next file from the file table */
bCont = FindNextFile(fFile,&fileInfo);
}
}
return 0;
}