Jump to content
  • Advertisement
Sign in to follow this  
Acharis

Cross platform directory list

This topic is 2205 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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).

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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")?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!