[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Recursive file & directory search

Posted on 2005-04-15
32
Medium Priority
?
1,330 Views
Last Modified: 2010-04-15
Hello All,

            I want to write a program for recursive file & dirctory search and also want to create some file at specific location. Can I know how can I write that in "C"?

Thanks in Advance .....

Hoping for best .....

Regards,
Pralay Desai
0
Comment
Question by:pralay
  • 16
  • 10
26 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13790945
Hi pralay,

This depends on your compiler and on your OS.
What compiler are you using, and what is your OS.

Most compilers have findfile function or POSIX opendir functions that can be used for searching for files/directories.

David Maisonave :-)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13791028
Here's example code for using opendir api function.

#include <sys/types.h>
#include <dirent.h>

....
     /* I have changed the variable names for clarity - while a
        good reference, Stevens has the widespread and annoying
       habit of using cryptic variable names that only mean
       something when you already know what the code does.  */

     DIR * theDirectory;
     struct dirent * aDirectoryEntry;

     /* opendir opens a directory, in this case for reading. */
     theDirectory = opendir("/your/path/here/");

     /* NULL is returned on error.  I'll skip error handling for
        brevity's sake */

     /* successive calls to 'readdir' for the same DIR*
       will return in sequence all contents of the directory,
        with the end of the list signalled by returning a NULL.
       Unfortunately, this is also how an error is indicated.
    */
     while (NULL != (aDirectoryEntry = readdir(theDirectory))
    {
        /* the dirent structure has only a couple fields:
               struct dirent {
                    ino_t d_ino;        file's inode #
                    char  d_name;        file's name
            };
        */

        printf("%s\n", aDirectoryEntry->d_name);
     }
0
 
LVL 30

Expert Comment

by:Axter
ID: 13791069
Here's a findfirst example:

#include <stdio.h>
#include <dir.h>

int main(void)
{
    struct ffblk ffblk;
    int done;
   
    printf("Directory listing of *.*\n");
    done = findfirst("*.*",&ffblk,0);
    while (done!=-1)
    {
        printf(" %s\n", ffblk.ff_name);
        done = findnext(&ffblk);
    }
    return 0;
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Axter
ID: 13791092
For more information about findfirst, check out following link:
http://www.oucs.ox.ac.uk/documentation/userguides/c/l922.pdf

If you're programming in Windows, you can use Win32 API functions FindFirstFile or FindFirstFileEx
0
 
LVL 30

Expert Comment

by:Axter
ID: 13791118
pralay,
> >create some file at specific location.

To create a file, you can use fopen function.
To write to the file, you can use fputs and fwrite.

The above functions have stream buffering.

You can also use open and write functions which don't have buffering.

David Maisonave :-}
0
 

Author Comment

by:pralay
ID: 13791311
Hi all,

        Thanks for reply. Well I am using win Xp as os & I can use VC++ or Turbo C++ as compiler. So please let me know some code.

Hoping for some code .....

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13791356
For Turbo C++ and for VC++ you can use the findfirst function, which I posted example code for.

For VC++, you can also use the Win32 API functions FindFirstFile or FindFirstFileEx
0
 

Author Comment

by:pralay
ID: 13791378
How Can I use can also use the Win32 API functions FindFirstFile or FindFirstFileEx  for VC++?

Please let me know by some examples....

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13791441
Here's an example:

WIN32_FIND_DATA fd;
     HANDLE hFind = ::FindFirstFile (_T ("*.exe"), &fd);
     if (hFind != INVALID_HANDLE_VALUE)
     {
          do
          {
               TRACE(fd.cFileName);
                                       // Do some thing  
          } while (::FindNextFile (hFind, &fd));
          ::FindClose (hFind);
     }
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 13791451
Here's another Win32 example:

#include <windows.h>
#include <stdio.h>

int
main(int argc,char *argv[])
{
 char            *baseDir="C:\\*";
 WIN32_FIND_DATA dir;
 HANDLE          dp;

 if ( (dp=FindFirstFile(baseDir,&dir))==INVALID_HANDLE_VALUE ) {
   fprintf(stderr,"Can't list: %s, Error: %d\n",baseDir,GetLastError);
   return -1;
 }
 do {
   if ( dir.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY )
     printf("Dir:  %s\n",dir.cFileName);
   else
     printf("File: %s\n",dir.cFileName);
 } while ( FindNextFile(dp,&dir) );
 return 0;
}
0
 

Author Comment

by:pralay
ID: 13791544
I didnt get much in first example. Can I know bit about "char            *baseDir="C:\\*";" ?

What all need to run this? Is VC++ 6.0 is enough ? How do i create file ?

Regards,
Pralay Desai
0
 

Author Comment

by:pralay
ID: 13800341
Hello Axter & all,

                        I was trying findfirst example. But as its only gives me listing of current directory not the subdirectories with in it. Well what I am trying to do is What to search for whole c: drive including all directories and sub-directories. Then I want to create directories and files in some of the places.

                         So Can anyone give me how to do that?

Thanks in advance .....

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13801056
Then you need to create a recursive function, that will call itself every time it finds a sub directory.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13801074
Put your code into a function that takes the path as an argument, and then call that function every time you find a sub directory.

If you have problems getting it to work, then post the code, and I can further assist you.
0
 

Author Comment

by:pralay
ID: 13801086
How can I do that? I am sorry to ask you this question but right now I am bit tense.... If you can help me out with full code then it will be great.

This is what I want to do actually,

              I want to write program for checking write permission on whole C-drive and also want to log the path where write permission is there for user running the program to a text file..


Hoping for best .....

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13801296
>>If you can help me out with full code then it will be great.

I'm sorry, but full code would not be appropriate for a 50 question.
I've already answered your question, and given more then a A graade 50 point question worth.

But I'll still be happy to give you further assistance if you try doing the work, and then post your code if you get stuck.
0
 

Author Comment

by:pralay
ID: 13801323
Can you please let me know how can I keep track on directories? and I how i can pass it as an argument?

I thing I Can get this much answer right?

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13801372
>>Can you please let me know how can I keep track on directories?
I'm not sure what you mean by keep track of directories.

Are you looking to store all the directories?

It would be better if you just perform your required logic as you find each directory or file.

>> and I how i can pass it as an argument?

Post your code, and I can give you an example.
0
 

Author Comment

by:pralay
ID: 13801449
As I want to search whole c: drive. I am able to search in a directory. when search in one directory gets over pointer should move to next directory so that it can search in that so on ..... till last of drive.

Hoping for best .....

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 13801501
OK, Post your current code, and I can show you how to modified it for your requirements.
0
 

Author Comment

by:pralay
ID: 13957260
give me refund.
0
 

Author Comment

by:pralay
ID: 13957487
who feels that? But I was not able to get answer what I wanted.
0
 
LVL 30

Expert Comment

by:Axter
ID: 14150307
>>Axter - I think you may have gone way overboard in offering code samples -- definitely too far if this had been homework. And those 5 posts within 20 minutes at the beginning of the thread were a bit much, given that
>>the asker hadn't replied to clarify what was wanted.

This question should NOT be refunded, and the points should be awarded.

I answered the original question, moreover, I gave further feedback after the questioner added more details for his/her requirement.

The original question has been answered with more details then normally required for a 50 point question.
Please award the points
0
 
LVL 30

Expert Comment

by:Axter
ID: 14150328
Looking at the questioners history, he/she has only awared points one time, and at that time he/she awared a C grade to a 20 point question.

The other grade which is an A grade was forced accepted.

Out of the 12 questions posted, 5 of them have been deleted, and 5 are opened.  One question has been opened since 22 Mar 2005.

This is a very poor grading history, and (IMHO) the questioner does not deserved to continuously get refunded points, so that he/she would have more points to post more abandon questions.

pralay,
Please read the grading guidelines, and please have more respect and courtesy for those experts who are trying to help you.
0
 

Author Comment

by:pralay
ID: 14150886
Ok. If you all feel that then I am ok with giving points to Axter . Next time I will be more carefully while posting question.

Thanks Axter.

Regards,
Pralay Desai
0
 
LVL 30

Expert Comment

by:Axter
ID: 14152078
pralay,
>>Ok. If you all feel that then I am ok with giving points to Axter . Next time I will be more carefully while posting question.

Considering how much information I gave you, and considering I fully answered your original question, this question should have received an A grade.
This is especially so, since this is only a 50 point question.

When you have a poor grading history, experts will be reluctant to help you.


jmcg,
>>Axter, you've made your objection (twice!). The moderator handling this cleanup list will read your comments and decide.
My second post was not an objection to your recommendation.  My second post was an objection to the questioner's grading history, and he/she history of closing and awarding open questions.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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 reading files 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
Course of the Month18 days, 13 hours left to enroll

834 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