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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

directory search

Status
Not open for further replies.

labnotes

Technical User
Sep 1, 2003
33
CA
I am writing a program and require to search the whole hard
drive for files. I can search one directory but can't search though the subdirectories. Is there a function to do this. Is there also a function that will list all the directories and sub directories ?.

Thanks and have a great day
 
I've looked at FindFirstFile and FindNextFile the search the path but not the
complete tree unless I am missing something. Is there another API that
searches the whole directory tree for files?

Brin
 
what means complete tree? of course you're missing something. By the way, you could use GetOpenFileName (or FileDialog in MFC) if you want a windows object ready to use.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Some explanations about full dir tree searching: hdl = FindFirstFile("C:\\*.*",&win32_find_data) gets you search handle. Check file attribute field dwFileAttributes in WIN32_FIND_DATA structure for FILE_ATTRIBUTE_DIRECTORY bit. Don't forget skip "." and ".." entries. Wrap up search loop from FindFirstFile over FindNextFile to FindClose(hdl) in the recursive function. Invoke it for each subdirectory. Use every new search handle to scan subdirectory, then close it before return. Build search template for each subdirectory search. Add error handling. That's all. It's a cumbersom but straightforward approach.
No C/C++ standard directory search functions. There are many 3rd party dirscan libraries but it's not so hard to write your own...
 
Yes, ArkM is right, In addition to him I would suggest you to not recurse more than two levels of nesting

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
As a simple example (with no err checking)..
Code:
<----------SNIP........

WIN32_FIND_DATA wfd, dfd;
#define MAX_PATHSIZE     1000
#define MAX_NO_SUBDIRS   50
#define MAX_DIRNAME_SIZE 500
char directories[MAX_NO_SUBDIRS][MAX_DIRNAME_SIZE];

..
...
....
.....

void find_directories(int *nodirs)
// Locate any subdirectories under the current directory.
// We use FindFirstFile and FindNextFile testing the dwFileAttributes flag
// of the WIN32_FIND_DATA struct and store the directory names.
 { HANDLE dhdl = FindFirstFile (&quot;*.*&quot;,&dfd);
   int stop = FALSE, i = *nodirs;
   if (dhdl != INVALID_HANDLE_VALUE)
      if (dfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
       { if (strcmp(dfd.cFileName,&quot;.&quot;) && strcmp(dfd.cFileName,&quot;..&quot;))
          { //Don't load the link directories.
            strcpy(directories[i++],dfd.cFileName);
          }
       }
   else
      stop = TRUE;

   while (!stop)
    { if (!FindNextFile(dhdl, &dfd))
       { if (GetLastError() != ERROR_NO_MORE_FILES)
          { printf(&quot;Doh!...\n&quot;);
          }
         stop = TRUE;
       }
      else if (dfd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
       { if (strcmp(dfd.cFileName,&quot;.&quot;) && strcmp(dfd.cFileName,&quot;..&quot;))
          { //Don't load the link directories.
            strcpy(directories[i++],dfd.cFileName);
          }
       }
    }
   *nodirs = i;
 }

<--------- END SNIP...............
 
Thanks for the help. I have the search working to a point. I am not the administrator on the PC this will be running on and the program stops when I hit a directory that when I do a ls -l from the command prompt shows the directory as &quot;Permission denied&quot;. How can the program determine if it doesn't have permission to access a directory?

Thanks
 
Try GetLastError(), but you may simply skip searching if FindFirstFile returns INVALID_HANDLE_VALUE.
 
Use _show_last_error_a() or _show_last_error_w(). Put this code to use functions.

int _show_last_error_a()
{
char* x = 0;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
0,
(char*)&x,
0,
NULL
);
MessageBoxA(0, x, &quot;error:&quot;, MB_ICONEXCLAMATION);
//delete[] x;
return 0;
}
int _show_last_error_w()
{
wchar_t* x = 0;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
0,
(wchar_t*)&x,
0,
NULL
);
MessageBoxW(0, x, L&quot;error:&quot;, MB_ICONEXCLAMATION);
//delete[] x;
return 0;
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top