dir_nav_ex.cpp 2.05 KB
Newer Older
1
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
2
3
4
5
6
7
8
9
10
11
12
/*

    This is an example illustrating the use of the dir_nav component from the dlib C++ Library.
    It prints a listing of all directories and files in the users
    current working directory or the directory specified on the command line.  

*/


#include <iostream>
#include <iomanip>
13
#include <dlib/dir_nav.h>
Davis King's avatar
Davis King committed
14
15
#include <vector>
#include <algorithm>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

using namespace std;
using namespace dlib;


int main(int argc, char** argv)
{
    try
    {
        string loc;
        if (argc == 2)
            loc = argv[1];
        else
            loc = ".";  // if no argument is given then use the current working dir.
  
        directory test(loc);

Davis King's avatar
Davis King committed
33
34
        std::vector<directory> dirs;
        std::vector<file> files;
35
36
37
38
39
40
41
42
43

        cout << "directory: " << test.name() << endl;
        cout << "full path: " << test.full_name() << endl;        
        cout << "is root:   " << ((test.is_root())?"yes":"no") << endl;
        
        // get all directories and files in test
        test.get_dirs(dirs);
        test.get_files(files);

Davis King's avatar
Davis King committed
44
        // sort the files and directories
Davis King's avatar
Davis King committed
45
46
        sort(files.begin(), files.end());
        sort(dirs.begin(), dirs.end());
47
48
49
50

        cout << "\n\n\n";

        // print all the subdirectories
Davis King's avatar
Davis King committed
51
52
        for (unsigned long i = 0; i < dirs.size(); ++i)
            cout << "        <DIR>    " << dirs[i].name() << "\n";
53
54

        // print all the subfiles
Davis King's avatar
Davis King committed
55
56
        for (unsigned long i = 0; i < files.size(); ++i)
            cout << setw(13) << files[i].size() << "    " << files[i].name() << "\n";
57
58


Davis King's avatar
Davis King committed
59
60
        cout << "\n\nnumber of dirs:  " << dirs.size() << endl;
        cout << "number of files: " << files.size() << endl;
61
62

    }
Davis King's avatar
Davis King committed
63
    catch (file::file_not_found& e)
64
    {
Davis King's avatar
Davis King committed
65
        cout << "file not found or accessible: " << e.info << endl;
66
    }
Davis King's avatar
Davis King committed
67
    catch (directory::dir_not_found& e)
68
    {
Davis King's avatar
Davis King committed
69
        cout << "dir not found or accessible: " << e.info << endl;
70
    }
Davis King's avatar
Davis King committed
71
    catch (directory::listing_error& e)
72
73
74
75
76
77
    {
        cout << "listing error: " << e.info << endl;
    }
}