[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

file and directory listing

Posted on 2000-05-12
3
Medium Priority
?
722 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

656 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