This repository was archived by the owner on May 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrm.hpp
More file actions
47 lines (42 loc) · 1.5 KB
/
rm.hpp
File metadata and controls
47 lines (42 loc) · 1.5 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
#pragma once
#include <all.hpp>
class RmCommand : public liteshell::BaseCommand
{
public:
RmCommand()
: liteshell::BaseCommand(
"rm",
"Remove one or many files/directories",
"If any of the targets is a directory, remove it recursively",
liteshell::CommandConstraint("targets", "The targets to remove", true, true)) {}
DWORD run(const liteshell::Context &context)
{
for (auto &target : context.values.at("targets"))
{
auto targets = utils::list_files(target);
if (targets.empty())
{
auto message = utils::format("Warning: Target \"%s\" does not exist", target.c_str());
std::cerr << message << std::endl;
continue;
}
for (auto &target : targets)
{
if (target.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
utils::remove_directory(utils::utf_convert(target.cFileName), true);
}
else if (DeleteFileW(target.cFileName))
{
std::cout << "Deleted " << target.cFileName << std::endl;
}
else
{
auto message = utils::last_error(utils::format("Error deleting file \"%s\"", utils::utf_convert(target.cFileName).c_str()));
std::cerr << message << std::endl;
}
}
}
return 0;
}
};