LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

search files in a directory tree

Hi

I have a problem with the GetFirstFile / GetNextFile CVI function.
For searching some files in a directory tree I want to use the functions
recursively but if the function go
back to the next search in a base directory the GetNextFile function
returns a error -2.
Can anyone help or it is possible to use a another way to search files
in a directory tree ?

Thanks,
Thomas
0 Kudos
Message 1 of 6
(7,280 Views)
Hi Thomas -
Whenever you change directories you MUST use the GetFirstFile function first, even if you have already searched that directory before.

Can you have a metafunction that calls GetNextFile first, looks at the error message and then calls GetFirstFile if neccessary? I'm trying to think i recursion terms, you would need a metafunction capable of both instances.


good luck
ben schulte
national instruments
0 Kudos
Message 2 of 6
(7,280 Views)
Hi Ben

thanks for the tip.
I have added GetFirstFile after the recursive call of the search function
( its better for use in CVI debug mode as an call after a error return of
GetNextFile ). Furthermore I must use GetNextFile several times to put the
pointer in the next search position in the base directory.

That�s the new function:


void find(char *directory)
{
char ff[MAX_FILENAME_LEN];
int i,k,dir,info,posinpath;
char path[MAX_DIRNAME_LEN];
char name[MAX_DIRNAME_LEN+MAX_FILENAME_LEN];
char fname[MAX_FILENAME_LEN];
char basispath[MAX_DIRNAME_LEN];

SplitPath (directory, basispath, path, fname);
strcat (basispath, path);
posinpath=0;
i = GetFirstFile (directory, 1, 1, 1, 1, 1, 1, ff);
while (i==0)
{
sprintf(name,"%s%s",basispath,ff);
dir = GetFi
leAttrs (name, &info, &info, &info, &info);
if (dir==1)
{
sprintf(path,"%s\\%s",name,fname);
find(path); // recursive call
i = GetFirstFile (directory, 1, 1, 1, 1, 1, 1, ff);
for (k=0;k }
else
{
// use file
SetCtrlVal (ph, PANEL_TEXTBOX, name);
SetCtrlVal (ph, PANEL_TEXTBOX, "\r\n");
}
posinpath++;
i=GetNextFile(ff);
}
}


find(�C:\\WINDOWS\\*.*�);


�Find� search only in all subdirectory�s if the filename is �*.*�. For
specific files ( like �*.UIR� ) the selection must follow after the
�//use file� entry.


Thomas
0 Kudos
Message 3 of 6
(7,280 Views)
hallo thomas deutscher, 1. ich habe eben diese aufgabe - einen verzeichnisbaum rekursiv zu durchsuchen. da habe ich Euren dialog gesehen und verstehe nicht, wieso der mehrfache aufruf von GetFirstFile() notwendig ist ??? 2. In dem Codeauszug ist eine zeile for(k=0; k}. sollst das sein ? beste grüsse Simon Lange
0 Kudos
Message 4 of 6
(6,903 Views)

I know this is an old thread, but I created a recursive folder search routine based on the ideas here, and I thought someone else might like to have it.  My code is designed to iterate through a set of folders.  When it finds a new folder, it calls a passed-in function pointer to process the folder.  This way the recursion part is separated from the processing part.

 

typedef void (*SubFolderProcessor)(const char *const SubFolder);
static void IterateSubFolders(char *Path, SubFolderProcessor);

/*********************/
/* IterateSubFolders */
/*********************/
static void IterateSubFolders(char *Path, SubFolderProcessor FolderProcessor)
{
    int NumberOfFileMatches = 0;
    int FileWasFound;
   
    char SearchString[MAX_PATHNAME_LEN];
    char FoundFile[MAX_PATHNAME_LEN];
    char FullPathToFoundFile[MAX_PATHNAME_LEN];
    
    // create search string
    strcpy(SearchString, Path);
    strcat(SearchString, "\\*.*");
       
    // look for files
    FileWasFound = GetFirstFile (SearchString, 1, 0, 0, 0, 0, 1, FoundFile);
   
    // if we found something (note: here 0 means YES)...
    while (FileWasFound == 0)
    {
        int IsSubFolder;
        int junk1, junk2, junk3, junk4;
       
        // determine if it's a folder or a file
        sprintf(FullPathToFoundFile, "%s\\%s", Path, FoundFile);
        IsSubFolder = GetFileAttrs (FullPathToFoundFile, &junk1, &junk2, &junk3, &junk4);
       
        // if it is a folder...
        if (IsSubFolder)
        {
            int FileSearchMatches;
           
            // process the new folder
            FolderProcessor(FullPathToFoundFile);
           
            ////////////////////
            // RECURSIVE CALL //
            ////////////////////
            // iterate the new folder
            IterateSubFolders(FullPathToFoundFile, FolderProcessor);   
           
            /*
                The recursive call reset the GetFirstFile/GetNextFile mechanism,
                so we have to restart it for the parent path and then move past
                all the files/folders we've already examined.
            */
            FileWasFound = GetFirstFile (SearchString, 1, 0, 0, 0, 0, 1, FoundFile);
            for (FileSearchMatches = 0; FileSearchMatches < NumberOfFileMatches; ++FileSearchMatches)
            {
                GetNextFile(FoundFile);
            } /* for */
        } /* if-then */

 

        // increment the number of files/folders we've found
        ++NumberOfFileMatches;
       
        // get the next matching file/folder
        FileWasFound = GetNextFile(FoundFile);
    } /* if-then */
} /* IterateSubFolders */

Here's an example of how to use it:

 

/***********************/
/* ProcessNewSubFolder */
/***********************/
static void ProcessNewSubFolder(const char *const SubFolder)
{

    InsertTestBoxLine(panelHandle, PANEL_txtFolderList, -1, SubFolder);

} /* ProcessNewSubFolder */

 

... 

ResetTextBox(panelHandle, PANEL_txtFolderList, "");

IterateSubFolders("C:", ProcessNewSubFolder);

...

Message 5 of 6
(6,562 Views)

This thread seems to have resurfaced, You could use the FileSelectPopup() routine from the user interface library, it works great, you can also use the GetDir, GetProjectDir, or GetFullPathFromProject functions before calling the fileselectpopups. You can tell the popup which folder to open with by default then the user can search all folders like a regular windows save/open window. If you need more help using the functions I described, I'd be happy to help. Hopefully this helps, good luck!

 

Cheers!

Doug

Douglas Sappet
Test Hardware Development Engineer
Allegro Microsystems Inc.
Manchester, NH
0 Kudos
Message 6 of 6
(6,536 Views)