Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursive Directory search

0 views
Skip to first unread message

Harald Harders

unread,
Sep 15, 2002, 11:25:43 AM9/15/02
to
Hello,

I am programming some code that should be as portable as possible.
I need a recursive directory search. For unix using gcc I have done it
with POSIX function calls (opendir, readdir, stat). But now, I have heard
that at least Microsoft C does not support these commands. Since I don't
have a Microsoft compiler I need your help to get a recursive directory
search, that works with MSC.

Maybe the POSIX code helps you to understand what I exactly want:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#define PATHSEP "/"

void recursivedir(const char *path)
{
DIR *dir;

printf("%s\n",path);

dir = opendir(path);
if (dir)
{
struct dirent *direntry;
struct stat buf;

while ((direntry=readdir(dir))!=NULL)
{
char *fulldir = malloc(sizeof(char)*
(strlen(path)+strlen(direntry->d_name)+2));
strcpy(fulldir,path);
strcat(fulldir,PATHSEP);
strcat(fulldir,direntry->d_name);
stat(fulldir,&buf);
if ( (S_ISDIR(buf.st_mode)) &&
(strcmp(direntry->d_name,".")!=0) &&
(strcmp(direntry->d_name,"..")!=0) )
recursivedir(fulldir);
free(fulldir);
} /* while */
closedir(dir);
} /* if */
}/* recursivedir */


int main()
{
recursivedir(".");
return 0;
}/* main */


Thank you in advance,
Harald

--
Harald Harders Langer Kamp 8
Institut für Werkstoffe D-38106 Braunschweig
Technische Universität Braunschweig Germany
E-Mail: h.ha...@tu-bs.de Tel: +49 (0)5 31 - 3 91 - 30 62
WWW : http://www.tu-bs.de/institute/ifw/ Fax: +49 (0)5 31 - 3 91 - 30 58

Kenneth Lantrip

unread,
Sep 15, 2002, 1:13:14 PM9/15/02
to

I have some working code you can use in a music player catalog program.
Its intended purpose is to scan a drive (or directory) recursively for
music files (mp3, ogg, etc). It works in windows only right now. You
might be able to port the part you need into whatever.

You can find it at http://bothersome.home.mindspring.com/mmp.html

The function you're looking for is AddFiles and ScanForFile.

Kip Warner

unread,
Sep 15, 2002, 1:42:28 PM9/15/02
to
Harald Harders wrote:

> Hello,
>
> I am programming some code that should be as portable as possible.
> I need a recursive directory search. For unix using gcc I have done it
> with POSIX function calls (opendir, readdir, stat). But now, I have heard
> that at least Microsoft C does not support these commands. Since I don't
> have a Microsoft compiler I need your help to get a recursive directory
> search, that works with MSC.
>

Hey man,

An outstanding compiler system you may wish to checkout is Dev-C++.
http://www.bloodshed.net/dev.

Kip

Harald Harders

unread,
Sep 16, 2002, 4:18:00 AM9/16/02
to
On Sun, 15 Sep 2002 12:13:14 -0500, Kenneth Lantrip wrote:
> Harald Harders wrote:
>> I am programming some code that should be as portable as possible.
>> I need a recursive directory search. For unix using gcc I have done it
>> with POSIX function calls (opendir, readdir, stat). But now, I have heard
>> that at least Microsoft C does not support these commands. Since I don't
>> have a Microsoft compiler I need your help to get a recursive directory
>> search, that works with MSC.

[...]

> I have some working code you can use in a music player catalog program.
> Its intended purpose is to scan a drive (or directory) recursively for
> music files (mp3, ogg, etc). It works in windows only right now. You
> might be able to port the part you need into whatever.
>
> You can find it at http://bothersome.home.mindspring.com/mmp.html
>
> The function you're looking for is AddFiles and ScanForFile.

Hi Kenneth,

thank you very much. By extracting some parts of you code, I got this:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

#define PATHSEP "\\"

void recursivedir(const char *path)
{
HANDLE FileHandle;
char *fullpath = malloc(sizeof(char)*(strlen(path)+2));

printf("%s\n",path);

strcpy(fullpath, path);
strcat(fullpath, PATHSEP);
strcat(fullpath, "*");

FileHandle = FindFirstFile(fullpath, &FindData);
if (FileHandle != INVALID_HANDLE_VALUE)
{
DWORD attr;
WIN32_FIND_DATA FindData;
do
{
if ( (attr & FILE_ATTRIBUTE_DIRECTORY) &&
(FindData.cFileName[0] != '.') )
{
char *fulldir = malloc(sizeof(char)*
(strlen(path)+
strlen(direntry->d_name)+2));
strcpy(fulldir, path);
strcat(np, PATHSEP);
strcat(fulldir, FindData.cFileName);
recursivedir(fulldir);
}/* if */
}/* do */
while (FindNextFile(FileHandle, &FindData) != 0);
FindClose(FileHandle);


} /* if */
}/* recursivedir */


int main()
{
recursivedir(".");
return 0;
}/* main */


Since I really have no access to a MS C compiler: Could somebody have a
look on that code if it compiles?

Thank you again,

Jerry Coffin

unread,
Sep 16, 2002, 3:32:39 PM9/16/02
to
In article <slrnao99jn.b...@pc52.ifw.ing.tu-bs.de>,
h.ha...@tu-bs.de says...

> Hello,
>
> I am programming some code that should be as portable as possible.
> I need a recursive directory search. For unix using gcc I have done it
> with POSIX function calls (opendir, readdir, stat). But now, I have heard
> that at least Microsoft C does not support these commands. Since I don't
> have a Microsoft compiler I need your help to get a recursive directory
> search, that works with MSC.

It sounds like some people have already pointed out FindFirstFile,
FindNextFile and FindClose to do the job you're talking about. Given
your stated goal of keeping your code as portable as possible, you
might prefer to continue using the POSIX functions, and using a
library that provides those functions on Win32 -- if so, you might
want to look at www.snippets.org, which has a library to do exactly
that (among quite a few other things).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Kenneth Lantrip

unread,
Sep 19, 2002, 7:40:18 PM9/19/02
to

There are quite a bit of errors in your code on first try to compile
with LCC-Win32. If you have access to a windows machine, you should get
the compiler from http://www.cs.virginia.edu/%7Elcc-win32/

The compiler is free so you could check out all the things that's wrong.

That link looks funny but it worked for me. There is also a link at the
bottom of my MMP page.

Harald Harders

unread,
Sep 20, 2002, 4:27:17 AM9/20/02
to
On Thu, 19 Sep 2002 18:40:18 -0500, Kenneth Lantrip wrote:
> Harald Harders wrote:
>> On Sun, 15 Sep 2002 12:13:14 -0500, Kenneth Lantrip wrote:
>>
>>>Harald Harders wrote:
>>>
>>>>I am programming some code that should be as portable as possible.
>>>>I need a recursive directory search. For unix using gcc I have done it
>>>>with POSIX function calls (opendir, readdir, stat). But now, I have heard
>>>>that at least Microsoft C does not support these commands. Since I don't
>>>>have a Microsoft compiler I need your help to get a recursive directory
>>>>search, that works with MSC.

[...]

>> thank you very much. By extracting some parts of you code, I got this:

[...]

>> Since I really have no access to a MS C compiler: Could somebody have a
>> look on that code if it compiles?
>

> There are quite a bit of errors in your code on first try to compile
> with LCC-Win32. If you have access to a windows machine, you should get
> the compiler from http://www.cs.virginia.edu/%7Elcc-win32/
>
> The compiler is free so you could check out all the things that's wrong.
>
> That link looks funny but it worked for me. There is also a link at the
> bottom of my MMP page.

Thanks for the link. I have downloaded it, and I will try it.

0 new messages