www.digitalmars.com         C & C++   DMDScript  

D - listdir?

reply "Mårten Ask" <majbritt37 hotmail.com> writes:
Hi,

How do I write a function that returns a list of all files in a directory?
Aug 02 2003
next sibling parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
import windows;
will give you

BOOL   FindClose(HANDLE hFindFile);
HANDLE FindFirstFileA(char *lpFileName, WIN32_FIND_DATA *lpFindFileData);
BOOL   FindNextFileA(HANDLE hFindFile, WIN32_FIND_DATA *lpFindFileData);

do a web search for FindFirstFile or look in the MSDN
FindFirstFileA is the ascii version
FindFirstFileW is the unicode version (wchar) you'll have to dl my win32 api
port
ans you'll need the W version of the WIN32_FIND_DATA

or if you want some fun, work out how to get an IShellFolder from your path,
and call EnumObjects , I can't remeber how to get anything but the Desktop
folder
SHGetDesktopFolder .... but to do it that way you might have to help me port
more of the win32 com headers :)


"Mårten Ask" <majbritt37 hotmail.com> wrote in message
news:bggpea$bkg$1 digitaldaemon.com...
 Hi,

 How do I write a function that returns a list of all files in a directory?
Aug 02 2003
prev sibling next sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
if using dig:

    Control.Search

or on linux you have to wrap dirent and stuff, a quick C++ function

void Dir::Read(vector<string> &files) {
 struct dirent* d;
 while (d = readdir(m_dir)) {
  if (strcmp(d->d_name,".") == 0) continue;
  else if (strcmp(d->d_name,"..")== 0) continue;
  else files.push_back(d->d_name);
 }
}

man readdir and man dirent should show what extern's you'll need to declare

Charles

"Mårten Ask" <majbritt37 hotmail.com> wrote in message
news:bggpea$bkg$1 digitaldaemon.com...
 Hi,

 How do I write a function that returns a list of all files in a directory?
Aug 02 2003
parent "Mårten Ask" <majbritt37 hotmail.com> writes:
Thanks!

I used Control.Search. It was by far the the most simple solution.



"Charles Sanders" <sanders-consulting comcast.net> skrev i meddelandet
news:bgh6ss$o7h$1 digitaldaemon.com...
 if using dig:

     Control.Search

 or on linux you have to wrap dirent and stuff, a quick C++ function

 void Dir::Read(vector<string> &files) {
  struct dirent* d;
  while (d = readdir(m_dir)) {
   if (strcmp(d->d_name,".") == 0) continue;
   else if (strcmp(d->d_name,"..")== 0) continue;
   else files.push_back(d->d_name);
  }
 }

 man readdir and man dirent should show what extern's you'll need to
declare
 Charles

 "Mårten Ask" <majbritt37 hotmail.com> wrote in message
 news:bggpea$bkg$1 digitaldaemon.com...
 Hi,

 How do I write a function that returns a list of all files in a
directory?

Aug 06 2003
prev sibling next sibling parent reply "Matthew Wilson" <matthew stlsoft.org> writes:
You can use the WinSTL findfile_sequence class, as in

int main()
{
  using winstl::findfile_sequence_a;

  findfile_sequence_a    files("somedir", "*.*",
findfile_sequence_a::files);
  std::vector<std::string> v;

  std::for_each(files.begin(), files.end(), std::back_inserter(v));

  . . . // do stuff with v
}

(Of course you can just use the findfile_sequence itself, rather than
copying to a vector<string) if that's appropriate. Dereferencing the
iterator (via operator *) yields an instance of
basic_findfile_sequence_value_type<C>, which has methods such as

    /// Returns a non-mutating reference to find-data
    find_data_type const    &get_find_data() const;
    /// Returns the filename part of the item
    char_type const         *get_filename() const;
    /// Returns the short form of the filename part of the item
    char_type const         *get_short_filename() const;
    /// Returns the full path of the item
    char_type const         *get_path() const;
    /// Implicit conversion to a pointer-to-const of the full path
    operator char_type const * () const;

  The get_path() is especially useful, and will yield a path relative to the
search directory given in the constructor of the sequence.
)

The findfile_sequence skips dots directories ("." & "..") by default, and is
able to give only directories, only files, or files and directores, by
specifiying the member enum values 'files' and 'directories'. There is a
third one 'includeDots' if you really want the dots.

Simple, eh?

fyi, WinSTL is a sub-project of STLSoft, which is bundled with DMC++ from
8.34 onwards (downloadable as a separate thing from
http://digitalmars.com/download/freecompiler.html if you've not got the CD).

-- 
Matthew Wilson

STLSoft moderator and C++ monomaniac

mailto:matthew stlsoft.org
http://www.stlsoft.org
news://news.digitalmars.com/c++.stlsoft

"So far, C++ is the best language I've discovered to say what I want to
say" -- Alex Stepanov

----------------------------------------------------------------------------
---


"Mårten Ask" <majbritt37 hotmail.com> wrote in message
news:bggpea$bkg$1 digitaldaemon.com...
 Hi,

 How do I write a function that returns a list of all files in a directory?
Aug 02 2003
parent "Matthew Wilson" <matthew stlsoft.org> writes:
ALERT! ALERT! FAILURE TO ENGAGE BRAIN!

I thought I was on the C++ ng. Sorry. That's what comes from checking the ng
before breakfast confers enough blood-sugar to constitute what just about
passes for a working brain. <blush>

ftr, I do have plans to do an STLSoft for D, but they're a long way off. :(


"Matthew Wilson" <matthew stlsoft.org> wrote in message
news:bghac4$rne$1 digitaldaemon.com...
 You can use the WinSTL findfile_sequence class, as in

 int main()
 {
   using winstl::findfile_sequence_a;

   findfile_sequence_a    files("somedir", "*.*",
 findfile_sequence_a::files);
   std::vector<std::string> v;

   std::for_each(files.begin(), files.end(), std::back_inserter(v));

   . . . // do stuff with v
 }

 (Of course you can just use the findfile_sequence itself, rather than
 copying to a vector<string) if that's appropriate. Dereferencing the
 iterator (via operator *) yields an instance of
 basic_findfile_sequence_value_type<C>, which has methods such as

     /// Returns a non-mutating reference to find-data
     find_data_type const    &get_find_data() const;
     /// Returns the filename part of the item
     char_type const         *get_filename() const;
     /// Returns the short form of the filename part of the item
     char_type const         *get_short_filename() const;
     /// Returns the full path of the item
     char_type const         *get_path() const;
     /// Implicit conversion to a pointer-to-const of the full path
     operator char_type const * () const;

   The get_path() is especially useful, and will yield a path relative to
the
 search directory given in the constructor of the sequence.
 )

 The findfile_sequence skips dots directories ("." & "..") by default, and
is
 able to give only directories, only files, or files and directores, by
 specifiying the member enum values 'files' and 'directories'. There is a
 third one 'includeDots' if you really want the dots.

 Simple, eh?

 fyi, WinSTL is a sub-project of STLSoft, which is bundled with DMC++ from
 8.34 onwards (downloadable as a separate thing from
 http://digitalmars.com/download/freecompiler.html if you've not got the
CD).
 -- 
 Matthew Wilson

 STLSoft moderator and C++ monomaniac

 mailto:matthew stlsoft.org
 http://www.stlsoft.org
 news://news.digitalmars.com/c++.stlsoft

 "So far, C++ is the best language I've discovered to say what I want to
 say" -- Alex Stepanov

 --------------------------------------------------------------------------
--
 ---


 "Mårten Ask" <majbritt37 hotmail.com> wrote in message
 news:bggpea$bkg$1 digitaldaemon.com...
 Hi,

 How do I write a function that returns a list of all files in a
directory?

Aug 02 2003
prev sibling parent Burton Radons <loth users.sourceforge.net> writes:
Mårten Ask wrote:
 How do I write a function that returns a list of all files in a directory?
The very first message I posted here ("http://www.digitalmars.com/drn-bin/wwwnews?D/4753") has a module for this.
Aug 02 2003