-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
151 lines (115 loc) · 4.58 KB
/
filesystem.cpp
File metadata and controls
151 lines (115 loc) · 4.58 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
void lexicalNormalRelativePath() {
std::filesystem::path foo =
std::filesystem::path(std::filesystem::current_path());
std::cout << foo.lexically_normal() << std::endl;
// std::cout << foo.lexically_proximate() << std::endl;
std::cout << std::filesystem::path(std::filesystem::current_path() / "../../")
.lexically_normal()
<< std::endl;
}
void filesystemExample() {
const auto FilePath{"FileToCopy"};
// If any filepath exists
if (std::filesystem::exists(FilePath)) {
const auto FileSize{std::filesystem::file_size(FilePath)};
std::filesystem::path tmpPath{"/tmp"};
// If filepath is available or not
if (std::filesystem::space(tmpPath).available > FileSize) {
// Create Directory
std::filesystem::create_directory(tmpPath.append("example"));
// Copy File to file path
std::filesystem::copy_file(FilePath, tmpPath.append("newFile"));
}
}
// fs::path("a/./b/..").lexically_normal() == "a/"
}
void directoryIterator() {
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
"foo/dir1");
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
"foo/dir1/child1");
std::filesystem::create_directories(std::filesystem::temp_directory_path() /
"foo/dir2");
std::filesystem::current_path(std::filesystem::temp_directory_path() / "foo");
std::ofstream{std::filesystem::current_path() / "foo/dir1/file1.txt"};
std::ofstream{std::filesystem::current_path() / "foo/dir2/file2.txt"};
for (auto const &iter :
std::filesystem::directory_iterator{std::filesystem::current_path()}) {
/*
Socket (S_IFSOCK)
Symbolic link (S_IFLNK)
Regular File (S_IFREG)
Block special file (S_IFBLK)
Directory (S_IFDIR)
Character device (S_IFCHR)
FIFO (named pipe) (S_IFIFO)
*/
std::cout << iter.path() << std::endl;
if (std::filesystem::is_directory(iter)) {
std::cout << iter.path().filename().string() << " is a directory"
<< std::endl;
}
if (std::filesystem::is_regular_file(iter)) {
std::cout << iter.path().filename().string() << " is a file" << std::endl;
}
}
std::cout << "Iterating " << std::filesystem::current_path() << " recursively"
<< '\n';
for (auto const &dir_entry : std::filesystem::recursive_directory_iterator{
std::filesystem::current_path()}) {
std::cout << dir_entry << '\n';
}
// std::ranges::for_each(
// std::filesystem::directory_iterator{std::filesystem::current_path()},
// [](const auto &dir_entry) { std::cout << dir_entry << '\n'; });
try {
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
"foo/dir1/child1");
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
"foo/dir1");
std::filesystem::remove_all(std::filesystem::temp_directory_path() /
"foo/dir2");
} catch (std::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "exception" << std::endl;
}
}
template <typename TP> std::time_t to_time_t(TP tp) {
using namespace std::chrono;
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() +
system_clock::now());
return system_clock::to_time_t(sctp);
}
int main(int argc, char **argv) {
//
// directoryIterator();
// lexicallyNormalRelativePath();
// fs::remove(p);
for (auto const &iter :
std::filesystem::directory_iterator{std::filesystem::current_path()}) {
if (std::filesystem::is_regular_file(iter)) {
std::cout << iter.path().filename().extension() << std::endl;
std::cout << iter.path().filename() << std::endl;
std::cout << "size = " << std::filesystem::file_size(iter) << '\n';
std::cout << "last_write_time = "
<< std::filesystem::last_write_time(iter) << '\n';
}
}
std::filesystem::file_time_type file_time =
std::filesystem::last_write_time(__FILE__);
std::time_t tt = to_time_t(file_time);
std::tm *gmt = std::gmtime(&tt);
std::stringstream buffer;
buffer << std::put_time(gmt, "%A, %d %B %Y %H:%M");
std::string formattedFileTime = buffer.str();
std::cout << formattedFileTime << '\n';
}
/*
How to add a path with "\"
std::string path{R"(C:\Program Files\DB Browser for SQLite\foo.db)"};
*/