?
Solved

file and directory listing

Posted on 2000-05-12
3
Medium Priority
?
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

741 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