How do you get a directory listing in C?

How do you scan a directory for folders and files in C? It needs to be cross-platform.

Directory listing in C on Windows [duplicate]

Possible Duplicate: How do you get a directory listing in C? How do you write a program which lists the directories recursively just like dir /s C:/ in the command prompt?

How do you get a directory listing sorted by creation date in python?

What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?

How do you get a directory listing sorted by creation date in PHP?

I have to list directories by using scandir() function but result array should be sorted according to date and time of directory created. Regards Deepak

How do I get a directory listing in Perl?

I would like to execute ls in a Perl program as part of a CGI script. For this I used exec(ls), but this does not return from the exec call. Is there a better way to get a listing of a directory in Pe

How do i get a directory listing for a folder on the web?

How do I get a directory listing for a folder on the web? I’m looking to download a group of small files from a folder on the web. I can do it easily with a a single file but I’m not sure how to do it

How do you get the current directory in compact framework?

How do you get the current directory where your app is running?

C# HttpWebRequest command to get directory listing

I need a short code snippet to get a directory listing from an HTTP server. Thanks

How do you get the creation date of files in a directory in Objective c

I’m learning Objective-C and I’m converting an Applescript utility to Objective-C. I need to understand how to get the NSFileCreationDate of all files in the directory so I can copy the newest file to

How do you iterate through every file/directory recursively in standard C++?

How do you iterate through every file/directory recursively in standard C++?

How can I enumerate all the file in a directory in vfs c or c++? [duplicate]

This question already has an answer here: How do you get a directory listing in C? 8 answers Recursive function for listing all files in sub directories 4 answers I need to enumerate all th


opendir/readdir are POSIX. If POSIX is not enough for the portability you want to achieve, check Apache Portable Runtime

The following will print the names of the files in the current directory:

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

int main (void)
  DIR *dp;
  struct dirent *ep;     
  dp = opendir ("./");

  if (dp != NULL)
    while (ep = readdir (dp))
      puts (ep->d_name);

    (void) closedir (dp);
    perror ("Couldn't open the directory");

  return 0;


The strict answer is “you can’t”, as the very concept of a folder is not truly cross-platform.

On MS platforms you can use _findfirst, _findnext and _findclose for a ‘c’ sort of feel, and FindFirstFile and FindNextFile for the underlying Win32 calls.

Here’s the C-FAQ answer:

There is no standard C (or C++) way to enumerate files in a directory.

Under Windows you can use the FindFirstFile/FindNextFile functions to enumerate all entries in a directory. Under Linux/OSX use the opendir/readdir/closedir functions.

Directory listing varies greatly according to the OS/platform under consideration. This is because, various Operating systems using their own internal system calls to achieve this.

A solution to this problem would be to look for a library which masks this problem and portable. Unfortunately, there is no solution that works on all platforms flawlessly.

On POSIX compatible systems, you could use the library to achieve this using the code posted by Clayton (which is referenced originally from the Advanced Programming under UNIX book by W. Richard Stevens). this solution will work under *NIX systems and would also work on Windows if you have Cygwin installed.

Alternatively, you could write a code to detect the underlying OS and then call the appropriate directory listing function which would hold the ‘proper’ way of listing the directory structure under that OS.

GLib is a portability/utility library for C which forms the basis of the GTK+ graphical toolkit. It can be used as a standalone library.

It contains portable wrappers for managing directories. See Glib File Utilities documentation for details.

Personally, I wouldn’t even consider writing large amounts of C-code without something like GLib behind me. Portability is one thing, but it’s also nice to get data structures, thread helpers, events, mainloops etc. for free

Jikes, I’m almost starting to sound like a sales guy šŸ™‚ (don’t worry, glib is open source (LGPL) and I’m not affiliated with it in any way)

The most similar method to readdir is probably using the little-known _find family of functions.

I’ve created an open source (BSD) C header that deals with this problem. It currently supports POSIX and Windows. Please check it out:

tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");

while (dir.has_next)
    tinydir_file file;
    tinydir_readfile(&dir, &file);

    if (file.is_dir)



#include <dirent.h>

DIR *d = opendir(./content/articles);
  printf(unable to open articles);
  struct dirent *entry;
  entry = readdir(d);
  if(! entry)
  printf(%sn, entry->d_name);