Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WinXP prob. with recursive directory listing function

Status
Not open for further replies.

andylec

Technical User
Feb 2, 2002
110
GB
I'm trying to list all folders on a system using a recursive function.

The following function works fine with Win 9x systems, but has problems on XP (not tried on NT or 2000). It gets to a protected folder named "System Volume Information" and seems to start again from the first folder it found.
Code:
// Usage: ListFolders( "C:\\" )
BOOL ListFolders( TCHAR *tcRoot )
{
    WIN32_FIND_DATA WFD ;
    HANDLE          hSearch ;
    TCHAR           tcFolder[MAX_PATH] ;

    // display directory
    cout << tcRoot << endl ;

    SetCurrentDirectory( tcRoot ) ;

    // start search
    hSearch = FindFirstFile( &quot;*.*&quot;, &WFD ) ;
    if (hSearch == INVALID_HANDLE_VALUE)
        return FALSE ;

    while ( FindNextFile( hSearch, &WFD ) )
    {
        // only if it is a directory
        if ( ( ( WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) )
            && ( strcmp( WFD.cFileName, &quot;..&quot; ) 
            && strcmp( WFD.cFileName, &quot;.&quot; ) ) )
        {
            GetCurrentDirectory( MAX_PATH, tcFolder ) ;

            // add trailing &quot;\&quot; if missing
            if ( strncmp( &tcFolder[strlen( tcFolder )-1], &quot;\\&quot;, 1 ) )
                (void) strcat( tcFolder, &quot;\\&quot; ) ;

            // create new path
            (void) strcat( tcFolder, WFD.cFileName ) ;

            // recursive call with new path
            ListFolders( tcFolder ) ;

            // go back one level
            SetCurrentDirectory( &quot;..&quot; ) ;

        } // end if

    } // end while

    // end search
    (void) FindClose( hSearch ) ;

    return TRUE ;
}
Any ideas what I can do to fix this?

rgds
Andy
 
Try this code,it should list all folders and files on a system.

#include <windows.h>
#include <iostream>
#include <direct.h>
#include <string.h>

void SearchDirectory(char *szFilename,char *szDirectory)
{
WIN32_FIND_DATA finder;
HANDLE hFile;

char Directory[_MAX_PATH];
char temp[1028];
char *fileName;
static int counter=1;
static int match=0;
static int repeat=0;
bool bImagesExist;
bool bWorking;
if(repeat==0)
{
_chdir(szDirectory);
}
repeat++;
sprintf(temp,&quot;%s&quot;,szFilename);
(hFile=FindFirstFile(temp,&finder)) ?
bImagesExist=true : bImagesExist=false;

while(bImagesExist)
{
(FindNextFile(hFile,&finder)) ?
bImagesExist=true : bImagesExist=false;

fileName = finder.cFileName;
sprintf(temp,&quot;Searching %s in %s : %d file(s)searched\n&quot;,(LPCTSTR)fileName,szDirectory,counter++);
cout<<temp;
if( !strstr((LPCTSTR) finder.cFileName,szFilename))
{
match++;
cout<<&quot;FOUND IN &quot;<<_getcwd(Directory,_MAX_PATH)<<endl
<<match<<&quot; file(s) match your request&quot;<<endl<<endl;
}
}

(hFile=FindFirstFile(&quot;*.*&quot;,&finder)) ?
bWorking=true : bWorking=false;
while(bWorking)
{
FindNextFile( hFile,&finder) ?
bWorking=true : bWorking=false;

if(finder.cFileName != &quot;.&quot; && finder.cFileName != &quot;..&quot;)
{
if ( finder.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )
{
_chdir((LPCTSTR)finder.cFileName);
SearchDirectory(szFilename,szDirectory);
}
}
}
_chdir(&quot;..&quot;);
FindClose(hFile);
}

int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
SearchDirectory(&quot;*.*&quot;,&quot;c:\\&quot;);
system(&quot;pause&quot;);
return 0;
}
 
you should also replace the line &quot;if( !strstr((LPCTSTR) finder.cFileName,szFilename))&quot; by

if( strstr((LPCTSTR) finder.cFileName,szFilename))

 
Close, but by using finder.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
it misses any folders marked as Read-Only, System, etc..

If I change the bitwise-AND operator to '==' in the code I was trying to use it has the same effect.

rgds
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top