Searching Files in a directory

Posted on 1998-11-18
Last Modified: 2010-04-15
What is the C function is used to search for files in the directory or subdirectory? Theare is a function ' stat ' for which if you give the file name we can get the information about that file whether it is ordinary file or a directory etc. Like this is there any function which searchs for the files in the directory?
Question by:jsreedhar
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
  • 2

Author Comment

ID: 1254407
Let me know the answer as early as possible.
Thank you.

Author Comment

ID: 1254408
Let me know the answer as early as possible,
Thank you.

Expert Comment

ID: 1254409
1.  What operating system are you using?
2.  What exactly do you mean by "search"?  Determining if a filename exists?  Listing all files that meet certain criteria?  Listing all files in a given directory?
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!

LVL 10

Accepted Solution

rbr earned 100 total points
ID: 1254410
Use opendir to open the directory structur and readdir to read ony entry. with stat (fstat) you can check the type of the entry (file or subdir).
LVL 10

Expert Comment

ID: 1254411
DIR *pdir
struct dirent *pentry;
struct stat stat;

pdir=opendir ("/");
while (NULL != (pentry = readdir (pdir))) {
      stat (pdir->d_name,&stat);
      if (stat.st_mode & S_IFDIR) {
            printf ("Directory: %s",pdir->d_name);


Author Comment

ID: 1254412
I am sorry, If I am not clear in the question. The question is like this. If I give a pattern say
' *.* ', it should list all the file names in the current directory and the sub-directories. Like in windows sdk programming we have FindFirstFile and FineNextFile which helps for doing the above. Same way is there any functions in C language. Actually I want write for both dos and unix. That is why I am using only C functions.

Thank you.

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

632 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