Solved

file and directory listing

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

Industry Leaders: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion 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.

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