www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How does listdir work?

reply Joey Peters <Joey_member pathlink.com> writes:
I'm making my own (small) build system and when I wanted to write something to
autogenerate the make scripts it seemed that listdir didn't work quite the way I
expected it to work. Phobos claims to just return the contents of a directory.
When I tried:

char[][] files = listdir(getcwd());
foreach(char[] file; files) {
std.stream.stdout.writeLine(file);
}

It seems to only return the relative path name instead of the directory
contents.

~ "/*" doesn't work either, ~"\\" neither on that case...

Anyway, does anyone know how to fix this problem, or have a work around? I want
to keep it portable.

-Joey
Sep 07 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Joey Peters wrote:
 I'm making my own (small) build system and when I wanted to write something to
 autogenerate the make scripts it seemed that listdir didn't work quite the way
I
 expected it to work. Phobos claims to just return the contents of a directory.
 When I tried:
 
 char[][] files = listdir(getcwd());
 foreach(char[] file; files) {
 std.stream.stdout.writeLine(file);
 }
 
 It seems to only return the relative path name instead of the directory
 contents.
 
 ~ "/*" doesn't work either, ~"\\" neither on that case...
 
 Anyway, does anyone know how to fix this problem, or have a work around? I want
 to keep it portable.
 
 -Joey
I think there's a bug in getcwd. This example works around the bug: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); cwd = cwd[0..cwd.length-1]; writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Apparently, getcwd returns a string contains a trailing null. This null seems to throw off listdir. Also, you might want to use std.recls to traverse directories: http://www.dsource.org/tutorials/index.php?show_example=27 -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 07 2004
parent reply Joey Peters <Joey_member pathlink.com> writes:
I think there's a bug in getcwd. This example works around the bug:

import std.file;
import std.stdio;

void main()
{
     char[][] d;
     char[] cwd = getcwd();
     cwd = cwd[0..cwd.length-1];

     writef("cwd: %s\n\n", cwd);

     d = listdir(cwd);
     for(int i; i<d.length; i++)
         writef("%s\n", d[i]);
}

Apparently, getcwd returns a string contains a trailing null. This null 
seems to throw off listdir.

Also, you might want to use std.recls to traverse directories:
http://www.dsource.org/tutorials/index.php?show_example=27
Thanks that worked fine :)
-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
Sep 08 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Joey Peters wrote:
I think there's a bug in getcwd. This example works around the bug:

import std.file;
import std.stdio;

void main()
{
    char[][] d;
    char[] cwd = getcwd();
    cwd = cwd[0..cwd.length-1];

    writef("cwd: %s\n\n", cwd);

    d = listdir(cwd);
    for(int i; i<d.length; i++)
        writef("%s\n", d[i]);
}

Apparently, getcwd returns a string contains a trailing null. This null 
seems to throw off listdir.
*Update* Now that Walter fixed the getcwd() bug with DMD 0.102, this is the proper code: import std.file; import std.stdio; void main() { char[][] d; char[] cwd = getcwd(); writef("cwd: %s\n\n", cwd); d = listdir(cwd); for(int i; i<d.length; i++) writef("%s\n", d[i]); } Also, this code based on the original post works: import std.file; import std.stream; void main() { char[][] files = listdir(getcwd()); foreach(char[] file; files) { std.stream.stdout.writeLine(file); } }
Also, you might want to use std.recls to traverse directories:
http://www.dsource.org/tutorials/index.php?show_example=27
Thanks that worked fine :)
-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Sep 21 2004
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
J C Calvarese wrote:
 Also, this code based on the original post works:
 
 import std.file;
 import std.stream;
 
 void main()
 {
     char[][] files = listdir(getcwd());
     foreach(char[] file; files) {
         std.stream.stdout.writeLine(file);
     }
 }
I just can't express how much I like foreach. It makes things so nice! I'd prefer to write the code above as this, which I think is a little simpler and easier to read: void main() { foreach(char[] file; listdir(getcwd)) std.stream.stdout.writeLine(file); } It's beautiful...
Sep 21 2004