Comment detail

指定されたフォルダ以下のゴミ掃除 (Nested Flatten)
boost::filesystemはあまり使ったことないので勉強がてら.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <boost/filesystem/fstream.hpp>

void removeBackupFile(const boost::filesystem::path& dir)
{
    namespace fs = boost::filesystem;
    if (fs::exists(dir)) {
        for (fs::directory_iterator i(dir), end; i != end; ++i) {
            if (fs::is_directory(i->status())) {
                removeBackupFile(i->path());
            } else {
                const std::string& name = i->path().leaf();
                if (name[name.size() - 1] == '~') {
//                    std::cout << "remove:" << i->path() << std::endl;
                    fs::remove(i->path());
                }
            }
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc == 1) {
        std::cerr << argv[0] << " dir" << std::endl;
        return 1;
    }
    try {
        removeBackupFile(argv[1]);
    } catch (boost::filesystem::filesystem_error& e) {
        std::cerr << "err:" << e.what() << std::endl;
    }
    return 0;
}

Index

Feed

Other

Link

Pathtraq

loading...