Codice PHP:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/param.h>

void do_a_directory(char pathnameint level)
{
   
DIR directory;                /* Like a FILE *, but for directories. */
   
struct dirent entry;          /* Only used as a pointer - see dirent.h */
   
char newname;                 /* Name of directory at next level. */
   
struct stat statbuf;            /* So we can check which files are directories. */
   
long where;                     /* We will leave this directory and come */
                                   /* back - need to remember where we were. */
   
int retvali;
   
/* Print this directory name, properly indented. */
   
for( 0leveli++)
      
printf("   ");
   
printf("%s\n"pathname);

   
/* Open a directory for directory reads. */
   
directory opendir(pathname);
   if(
directory == NULL)
   {
      
fprintf(stderr"Cannot open %s\n"pathname);
      
perror("Reason");
      return;
   }
   while( ( 
entry readdir(directory)) != NULL)
   {
      
/* Skip if . or .. */
      
if( strcmp(entry -> d_name".") == ||
            
strcmp(entry -> d_name"..") == )
         continue;
      
/* Manufacture pathname of entry relative to where we started. */
      
newname = (char *) malloc(MAXPATHLEN);
      
strcpy(newnamepathname);
      
strcat(newname"/");
      
strcat(newnameentry -> d_name);
      
/* stat it to see if it is a directory. */
      
retval stat(newname, &statbuf);
      if (
retval == &&  ( statbuf.st_mode S_IFDIR))
      {
         
/* If it is, close current directory, do the lower one recursively */
         /* and on return reopen the current directory, remembering to */
         /* get back to where we were. */
         
where telldir(directory);
         
closedir(directory);
         
do_a_directory(newnamelevel 1);
         
directory opendir(pathname);
         if(
directory == NULL)
         {
            
fprintf(stderr"Cannot open %s\n"pathname);
            return;
          }
          
seekdir(directorywhere);
      }
      
free(newname);
   }
   
closedir(directory);
}

void main(int argcchar argv[])
{
    
/* Usage check */
    
if( argc 2)
    {
        
fprintf(stderr,"Usage: dtree directory\n");
        exit(
1);
    }
    
do_a_directory(argv[1], 0);

la funzione do_a_directory stampa a video la directory tree a partire da una cartella radice, sia essa C:\ , D:\ , C:\prova\ciao, insomma qualsiasi cartella.
E' abbastanza banale inserire un modulo dentro la funzione do_a_directory() che cerchi esattamente il nome del file che stai ricercando, e stampi a video la cartella corrispondente solo se ha trovato il file.

codice preso da http://docs.linux.cz/programming/c/u...les/dtree.html

e già usato innumerevoli volte all'interno dei miei programmi