Cross platform directory list

Started by
18 comments, last by kop0113 11 years, 4 months ago
I need a crossplatform function to list a directory and then do something on the files inside (delete all files from there or copy files to another directory). I was doing it several decades ago and I'm not familiar how it's done nowadays, especially crosplatform :)


Example code (I think it was using <dos.h>):

void fClearDir(char* sp)
{
char sf[250];
char *n;
DIR *dirp;
struct dirent *direntp;
dirp = opendir( sp );
if( dirp != NULL ) {
for(;;) {
direntp = readdir( dirp );
if( direntp == NULL ) break;
n=direntp->d_name;
if (strcmp(n,".")!=0 && strcmp(n,"..")!=0)
{
strcpy(sf,sp);strcat(sf,n);
remove(sf);
}
}
closedir( dirp );
}
}


I need a modern replacement of this (crossplatform Windows+Mac+Linux).

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Advertisement
One option is boost::filesystem.
Well, I just need one small feature (iterate through directory) a full library would be an overkill in my case... Aren't there any standard functions for this? Something that could just go through all files in a directory regardless of platform?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube


Aren't there any standard functions for this?

No, the concept of file hierarchies is pretty platform-specific. Like the concept of a GUI, there is no standard way to do something that's not standard across platforms.

Unless you really think it's really necessary to have a hierarchical filesystem on a toaster. Or a phone.

Stephen M. Webb
Professional Free Software Developer

If you want to wait a few years, some standard functionality is working it's way through committee as part of TR2 ... based on boost::filesystem. Shouldn't be more than another five to ten years. Alternately you could use POSIX functions on the *nix platforms (opendir() and friends), and download a POSIX compatibility library for Windows.
Hmmm, it's more messy than I thought. OK, I can lower the requirements to "computers" only (Windows/Mac/Linux).

Does all Linux version use the same file hierarchy (I'm quite sure these does, just asking because of paranoia)?
Does Mac use the same file hierarchy (and therefore the same code can be used) as Linux?

Or to put shorter, can we narrow it to 2 versions of the code, one for Windows and one for Linux/Mac? Again, I'm just asking for basic iterate through a directory code.

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

Are you asking if they are the same file hierarchy or the same functions to enumerate directories? If you're asking the former, then no, different linux distributions and OS X will organize their directories differently. However, both Linux and OS X both support the POSIX functions for enumerating directories and files in directories (the previously mentioned opendir() and friends). The Windows API functions for enumerating entities within directories are FindFirstFile(Ex)() and related functions. If you want to support Mac classic on top of OS X then you've got another API you'll need to use. Or again you can download a library to emulate the POSIX calls on Windows and only need to write the code once.
I stumbled across something called "dirent.h" (apparently there is more than one version of this). How you evaluate it, is it good? If yes and you used it, can you drop a link to the implementation you used and can recommend?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

dirent.h is the POSIX header for directory functions, which contains opendir() and the other related functions that I've already told you about twice now. For Linux and OS X there's no need to download an implementation, it's part of the OS.
Oh, I understand, this is the "library" (consisting of exactly one .h file, which is extremely nice) to emulate POSIX on Windows and the other versions of dirent.h were the original files for *nix systems :) So, I just use this header for Windows version and for other versions I just use the default one.

Just to be sure, we are talking about this POSIX Windows API http://softagalleria.net/dirent.php and it will work with all IDEs (it says "Dirent API for Microsoft Visual Studio")?

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

This topic is closed to new replies.

Advertisement