Solved

file and directory listing

Posted on 2000-05-12
3
704 Views
Last Modified: 2010-04-02
I would like to list all files in current dir applying a function to each and if there is a directory list all files in that one and apply function to those and so on (recursive). Here is my current code,

void dircrawl(char *dir, void (*fcn)(char *, char *, char *))
{

   struct stat stbuf;
   struct _finddata_t file;  
   char name[MAX_PATH];
   long hFile;
   
   if((hFile = _findfirst("*.*", &file )) == -1){
        printf("Error no files in current directory\n"); }
   else{
      
           
 while( _findnext( hFile, &file ) == 0){
             stat(name, &stbuf);  
                  
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0) {
                   continue;
                        }
if (strlen(dir)+strlen(file.name)+2 > sizeof(name)){
                   fprintf(stderr, "dircrawl: name %s/%s too long \n", dir, file.name);}

                 
if (((stbuf.st_mode & S_IFMT) == S_IFDIR) && (_chdir(name)){                              sprintf(name, "%s/%s", dir, file.name);
                           (*fcn)(file.name, lookfor, name);
                            dircrawl(name, print);
                         }
else {
                        sprintf(name, "%s/%s", dir, file.name);
                              (*fcn)(file.name, lookfor, name);
                        
                        }
                  }  
                  _findclose( hFile );  
      
   }
}
0
Comment
Question by:texta
  • 3
3 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 2803882
/*
this function recursivly scan directries. .

i done this in VC++ on NT 4.0
*/

#include <stdio.h>
#include <io.h>


int scan_path(char *path)
{
     char spath[256];
     struct _finddata_t ff;
     long fhandle;
      
     sprintf(spath,"%s\\*",path);
     fhandle=_findfirst(spath,&ff);
     if(fhandle==-1L)
     {
          printf("Bad path\n");
          return -1;
     }
     else
     {
          /* loop through each etry in the dir */
          while(!_findnext(fhandle,&ff))
        {
               /* if its a sub-dir then recursivly call our self*/
               if(ff.attrib&_A_SUBDIR)
               {
                     if(strcmp(ff.name,".."))
                     {
                            sprintf(spath,"%s\\%s",path,ff.name);
                            scan_path(path2);
                     }
               }
               else
               {
                     /* if its a file then */
                     if(ff.attrib^_A_SYSTEM)
                     {
                            /* in here you will have to check the extension */
                            printf("Found File %s\n",ff.name);
                     }
               }
          }
     }
     return 1;
}
      
0
 
LVL 10

Expert Comment

by:makerp
ID: 2803885
just put a line under the found file bit that performs the task on the file. you may want to check the file is the right type. this cann be achived by interogating the ff.name var
0
 
LVL 10

Expert Comment

by:makerp
ID: 2825303
any luck ?
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question