Click here to Skip to main content
12,952,124 members (58,505 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.2K views
3 bookmarked
Posted 14 Sep 2011

Learning Poco: List Directories Recursively

, 14 Sep 2011 CC (ASA 3U)
Rate this:
Please Sign up or sign in to vote.
A simple utility program for listing directories recursively

In this tutorial, we will write a simple utility program for listing directories recursively. Poco provides DirectoryIterator to iterate over all the items contained in a directory. We will also make use of DateTimeFormatter to format the last-modified timestamp in our local time zone instead of the default UTC. This utility mimics the behavior of ls -all.

The Poco here stands for the POCO C++ libraries and is not to be confused with Plain Old CLR Object.

Create a file named rec_dir.cc with the following code:

#include <Poco/DirectoryIterator.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/LocalDateTime.h>
#include <iostream>
#include <string>

using namespace Poco;
using namespace std;

void rec_dir(const string & path)
{
  DirectoryIterator end;
  for (DirectoryIterator it(path); it != end; ++it)
  {
    cout << (it->isDirectory() ? "d" : "-");
    cout << (it->canRead()     ? "r" : "-");
    cout << (it->canWrite()    ? "w" : "-");
    cout << (it->canExecute()  ? "x" : "-");
    cout << "\t";

    cout << it->getSize() << "\t";

    LocalDateTime lastModified(it->getLastModified());
    cout << DateTimeFormatter::format
    (lastModified, "%Y-%m-%d %H:%M") << "\t";

    cout << it->path() << (it->isDirectory() ? 
    "/" : it->canExecute() ? "*" : "") << endl;

    if (it->isDirectory())
    {
      rec_dir(it->path());
    }
  }
}

int main(int argc, char **argv)
{
  rec_dir(argc > 1 ? argv[1] : ".");
  return 0;
}

Compile it and run:

$ g++ -o rec_dir rec_dir.cc -lPocoFoundation
$ ./rec_dir
-rwx        14223   2011-09-14 13:34        rec_dir*
drwx        4096    2011-09-14 12:52        testdir/
drwx        4096    2011-09-14 12:52        testdir/dir/
-rw-        941     2011-09-14 13:24        rec_dir.cc
-rw-        0       2011-09-14 12:43        somefile

You may also try ./rec_dir .. or ./rec_dir ~. It’s fun! :–)

The code is simple enough but these classes provided by Poco are powerful. You may check out DirectoryIterator and File for information about other usage.

Related Articles

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License

Share

About the Author

stfairy
Student Shanghai Jiao Tong University
China China
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 14 Sep 2011
Article Copyright 2011 by stfairy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid