NO

Author Topic: Whats the best way to walk a directory to list files?  (Read 5573 times)

Gary Willoughby

  • Guest
Whats the best way to walk a directory to list files?
« on: February 17, 2010, 12:21:17 AM »
Whats the best way to walk a directory to list files?

Basically, i have a starting directory and i want to grab the files from that directory and display them in a console. Do i need to use the os api commands for that? Is there a cross-platform way?

Any ideas or snippets?
« Last Edit: February 17, 2010, 12:25:14 AM by Gary Willoughby »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Whats the best way to walk a directory to list files?
« Reply #1 on: February 17, 2010, 08:03:32 AM »
opendir() readdir() closedir() are usable in linux too.
Remember to use compiler option Define compatibility names.

_findfirst(), _findnext() and _findclose() are MSVC-functions for that.

Code: [Select]
/* opendir example*/
#include <stdio.h>
#include <dirent.h>

int main(int argc, char **argv)
{
DIR *dir;
struct dirent *de;

dir = opendir(".");
while(dir) {
de = readdir(dir);
if (!de) break;
printf("%i %s\n", de->d_type, de->d_name);
}
closedir(dir);
return 0;
}
« Last Edit: February 17, 2010, 08:18:08 AM by timovjl »
May the source be with you

Gary Willoughby

  • Guest
Re: Whats the best way to walk a directory to list files?
« Reply #2 on: February 20, 2010, 06:11:00 PM »
Thanks a lot!

I had to change it to this to work in PellesC

Code: [Select]
#include <dirent.h>

int main(int argc, char **argv)
{
_DIR *dir;
struct _dirent *de;

dir = _opendir(".");
while(dir) {
de = _readdir(dir);
if (!de) break;
printf("%i %s\n", de->d_type, de->d_name);
}
_closedir(dir);
return 0;
}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Whats the best way to walk a directory to list files?
« Reply #3 on: February 20, 2010, 06:31:08 PM »
Remember to use compiler option Define compatibility names.
So use -Go  -> (oldnames.lib)
May the source be with you

Gary Willoughby

  • Guest
Re: Whats the best way to walk a directory to list files?
« Reply #4 on: February 20, 2010, 11:07:29 PM »
Ah right. What does that flag do?

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Whats the best way to walk a directory to list files?
« Reply #5 on: February 20, 2010, 11:20:53 PM »
What does that flag do?

Did you already check the Pelles C documentation of the compiler and linker options?
---
Stefan

Proud member of the UltraDefrag Development Team

Gary Willoughby

  • Guest
Re: Whats the best way to walk a directory to list files?
« Reply #6 on: February 21, 2010, 12:19:49 AM »
What does that flag do?

Did you already check the Pelles C documentation of the compiler and linker options?

Got it thanks! :)