www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use tango's FileScan correctly?

reply qwesx <qwesx qwesx.com> writes:
Hello!

I'm trying out some things in D (with tango) and tried to build a *very* 
primitive indexing program. One function requires to run through a 
directory (without recursion) and get me all the files/folders in there.

So after reading the documentation I found that tango.io.FileScan 
basically does what I need. It's just that I can't get it to work. All 
examples there use some filter, but I don't need one. And the other stuff 
I tried (and which compiled) gives me either a wrong number of files in 
my debug output, or none at all.

Here's (part of) my code:

FileScan scan = new FileScan();
// only one of those was used at a time, the others are commented
// dir is a char[] with an existing (absolute) directory:
scan(dir, "*");
scan(dir, ".*");
scan(dir, "*.*");
scan(dir, "");
scan = scan.sweep(dir, false);


FilePath[] myFolders = scan.folders();
FilePath[] myFiles = scan.files();
char[][] err = scan.errors();
Stdout("found folders: ")(myFolders.length)("\n")();
Stdout("found files  : ")(myFiles.length)("\n")();
Stdout("errors:\n")();
foreach (char[] e; err)
  Stdout(e)("\n")();


The result was always:

found folders: 0
found files  : 0
errors:
% _

I think I'm doing something very wrong here. And it's possibly because I 
don't understand the documentation.

Can you help me get this to work?



Regards
qwesx
Oct 14 2010
next sibling parent Moritz Warning <moritzwarning web.de> writes:
I suspect there is a bug.
You could use FilePath.toList() as an alternative for now.

foreach(path; FilePath(".").toList())
{
  if(path.isFolder)
    folder_counter++;
  else
    file_counter++;
}

On Thu, 14 Oct 2010 15:31:47 +0000, qwesx wrote:

 Hello!
 
 I'm trying out some things in D (with tango) and tried to build a *very*
 primitive indexing program. One function requires to run through a
 directory (without recursion) and get me all the files/folders in there.
 
 So after reading the documentation I found that tango.io.FileScan
 basically does what I need. It's just that I can't get it to work. All
 examples there use some filter, but I don't need one. And the other
 stuff I tried (and which compiled) gives me either a wrong number of
 files in my debug output, or none at all.
 
 Here's (part of) my code:
 
 FileScan scan = new FileScan();
 // only one of those was used at a time, the others are commented // dir
 is a char[] with an existing (absolute) directory: scan(dir, "*");
 scan(dir, ".*");
 scan(dir, "*.*");
 scan(dir, "");
 scan = scan.sweep(dir, false);
 
 
 FilePath[] myFolders = scan.folders(); FilePath[] myFiles =
 scan.files();
 char[][] err = scan.errors();
 Stdout("found folders: ")(myFolders.length)("\n")(); Stdout("found files
  : ")(myFiles.length)("\n")(); Stdout("errors:\n")();
 foreach (char[] e; err)
   Stdout(e)("\n")();
 
 
 The result was always:
 
 found folders: 0
 found files  : 0
 errors:
 % _
 
 I think I'm doing something very wrong here. And it's possibly because I
 don't understand the documentation.
 
 Can you help me get this to work?
 
 
 
 Regards
 qwesx
Oct 14 2010
prev sibling next sibling parent Moritz Warning <moritzwarning web.de> writes:
Here is the bug report (patch proposal included):

http://dsource.org/projects/tango/ticket/2003

On Thu, 14 Oct 2010 15:31:47 +0000, qwesx wrote:

 Hello!
 
 I'm trying out some things in D (with tango) and tried to build a *very*
 primitive indexing program. One function requires to run through a
 directory (without recursion) and get me all the files/folders in there.
 
 So after reading the documentation I found that tango.io.FileScan
 basically does what I need. It's just that I can't get it to work. All
 examples there use some filter, but I don't need one. And the other
 stuff I tried (and which compiled) gives me either a wrong number of
 files in my debug output, or none at all.
 
 Here's (part of) my code:
 
 FileScan scan = new FileScan();
 // only one of those was used at a time, the others are commented // dir
 is a char[] with an existing (absolute) directory: scan(dir, "*");
 scan(dir, ".*");
 scan(dir, "*.*");
 scan(dir, "");
 scan = scan.sweep(dir, false);
 
 
 FilePath[] myFolders = scan.folders(); FilePath[] myFiles =
 scan.files();
 char[][] err = scan.errors();
 Stdout("found folders: ")(myFolders.length)("\n")(); Stdout("found files
  : ")(myFiles.length)("\n")(); Stdout("errors:\n")();
 foreach (char[] e; err)
   Stdout(e)("\n")();
 
 
 The result was always:
 
 found folders: 0
 found files  : 0
 errors:
 % _
 
 I think I'm doing something very wrong here. And it's possibly because I
 don't understand the documentation.
 
 Can you help me get this to work?
 
 
 
 Regards
 qwesx
Oct 14 2010
prev sibling parent reply Moritz Warning <moritzwarning web.de> writes:
Ok,

as I've found out, FileScan is only for locating a set of files.
the preferred way is to use the virtual file system (VFS):

http://dsource.org/projects/tango/wiki/ChapterVFS

Examples:
http://www.dsource.org/projects/tango/wiki/VfsZipAndLinesExample
http://dsource.org/projects/tango/browser/trunk/doc/example/vfs/vfscan.d
http://dsource.org/projects/tango/browser/trunk/doc/example/vfs

But I think FilePath.toList()
is enough for your plans.


On Thu, 14 Oct 2010 15:31:47 +0000, qwesx wrote:

 Hello!
 
 I'm trying out some things in D (with tango) and tried to build a *very*
 primitive indexing program. One function requires to run through a
 directory (without recursion) and get me all the files/folders in there.
 
 So after reading the documentation I found that tango.io.FileScan
 basically does what I need. It's just that I can't get it to work. All
 examples there use some filter, but I don't need one. And the other
 stuff I tried (and which compiled) gives me either a wrong number of
 files in my debug output, or none at all.
 
 Here's (part of) my code:
 
 FileScan scan = new FileScan();
 // only one of those was used at a time, the others are commented // dir
 is a char[] with an existing (absolute) directory: scan(dir, "*");
 scan(dir, ".*");
 scan(dir, "*.*");
 scan(dir, "");
 scan = scan.sweep(dir, false);
 
 
 FilePath[] myFolders = scan.folders(); FilePath[] myFiles =
 scan.files();
 char[][] err = scan.errors();
 Stdout("found folders: ")(myFolders.length)("\n")(); Stdout("found files
  : ")(myFiles.length)("\n")(); Stdout("errors:\n")();
 foreach (char[] e; err)
   Stdout(e)("\n")();
 
 
 The result was always:
 
 found folders: 0
 found files  : 0
 errors:
 % _
 
 I think I'm doing something very wrong here. And it's possibly because I
 don't understand the documentation.
 
 Can you help me get this to work?
 
 
 
 Regards
 qwesx
Oct 14 2010
parent qwesx <qwesx qwesx.com> writes:
On Thu, 14 Oct 2010 18:07:20 +0000, Moritz Warning wrote:

 Ok,
 
 as I've found out, FileScan is only for locating a set of files. the
 preferred way is to use the virtual file system (VFS):
 
 http://dsource.org/projects/tango/wiki/ChapterVFS
 
 Examples:
 http://www.dsource.org/projects/tango/wiki/VfsZipAndLinesExample
 http://dsource.org/projects/tango/browser/trunk/doc/example/vfs/vfscan.d
 http://dsource.org/projects/tango/browser/trunk/doc/example/vfs
 
 But I think FilePath.toList()
 is enough for your plans.
I'll take a look at it. Thank all three of you, I got it working with toList() :-) Regards qwesx
Oct 14 2010