1 // simple_ls program -------------------------------------------------------//
3 // Copyright Jeff Garland and Beman Dawes, 2002
5 // Use, modification, and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/filesystem for documentation.
11 #define BOOST_FILESYSTEM_VERSION 3
13 // As an example program, we don't want to use any deprecated features
14 #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
15 # define BOOST_FILESYSTEM_NO_DEPRECATED
17 #ifndef BOOST_SYSTEM_NO_DEPRECATED
18 # define BOOST_SYSTEM_NO_DEPRECATED
21 #include <boost/filesystem/operations.hpp>
22 #include <boost/filesystem/directory.hpp>
23 #include <boost/filesystem/path.hpp>
26 namespace fs
= boost::filesystem
;
28 int main(int argc
, char* argv
[])
30 fs::path
p(fs::current_path());
33 p
= fs::system_complete(argv
[1]);
35 std::cout
<< "\nusage: simple_ls [path]" << std::endl
;
37 unsigned long file_count
= 0;
38 unsigned long dir_count
= 0;
39 unsigned long other_count
= 0;
40 unsigned long err_count
= 0;
44 std::cout
<< "\nNot found: " << p
<< std::endl
;
48 if (fs::is_directory(p
))
50 std::cout
<< "\nIn directory: " << p
<< "\n\n";
51 fs::directory_iterator end_iter
;
52 for (fs::directory_iterator
dir_itr(p
);
58 if (fs::is_directory(dir_itr
->status()))
61 std::cout
<< dir_itr
->path().filename() << " [directory]\n";
63 else if (fs::is_regular_file(dir_itr
->status()))
66 std::cout
<< dir_itr
->path().filename() << "\n";
71 std::cout
<< dir_itr
->path().filename() << " [other]\n";
75 catch (const std::exception
& ex
)
78 std::cout
<< dir_itr
->path().filename() << " " << ex
.what() << std::endl
;
81 std::cout
<< "\n" << file_count
<< " files\n"
82 << dir_count
<< " directories\n"
83 << other_count
<< " others\n"
84 << err_count
<< " errors\n";
86 else // must be a file
88 std::cout
<< "\nFound: " << p
<< "\n";