Solved

file and directory listing

Posted on 2000-05-12
3
696 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now