-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_orphaned.cpp
104 lines (82 loc) · 2.55 KB
/
delete_orphaned.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// Place this compiled at Obsidian vault root directory
// Replace 'Prisegtukai' (attachments) with whatever (I assume you use a folder for attachments, why wouldn't you),
// use code editor's find-replace function.
// This program likely does not support UTF-8 filenames, depending on your compiler's implementation.
// This program also heavily relies on system calls, so while not great and hacky, it does it thing.
int main()
{
system("mkdir cleanup");
system("dir /b /ad > cleanup\\directories.txt");
system("dir /b > cleanup\\files.txt");
system("dir /b Prisegtukai > cleanup\\attachments.txt");
vector<string> Directories, Files, Attachments;
ifstream GetDirectories;
GetDirectories.open("cleanup\\directories.txt");
while (!GetDirectories.eof())
{
string Directory;
getline(GetDirectories, Directory);
Directories.push_back(Directory);
}
Directories.pop_back();
GetDirectories.close();
ifstream GetFiles;
GetFiles.open("cleanup\\files.txt");
while (!GetFiles.eof())
{
string File;
getline(GetFiles, File);
Files.push_back(File);
}
Files.pop_back();
GetFiles.close();
ifstream GetAttachments;
GetAttachments.open("cleanup\\attachments.txt");
while (!GetAttachments.eof())
{
string Attachment;
getline(GetAttachments, Attachment);
Attachments.push_back(Attachment);
}
Attachments.pop_back();
GetAttachments.close();
for (unsigned i = 0; i < Directories.size(); ++i)
{
Files.erase(remove(Files.begin(), Files.end(), Directories[i]), Files.end());
}
Files.erase(remove(Files.begin(), Files.end(), "delete_orphaned.cpp"), Files.end());
Files.erase(remove(Files.begin(), Files.end(), "delete_orphaned.exe"), Files.end());
vector<string> NotOrphans;
ifstream ImpFiles;
for (unsigned i = 0; i < Files.size(); ++i)
{
string File;
ImpFiles.open(Files[i]);
while (!ImpFiles.eof())
{
string Str;
ImpFiles >> Str;
File += Str + ' ';
}
ImpFiles.close();
for (unsigned j = 0; j < Attachments.size(); ++j)
{
if (File.find(Attachments[j]) != string::npos) {
NotOrphans.push_back(Attachments[j]);
}
}
}
for (unsigned i = 0; i < Attachments.size(); ++i)
{
if (find(NotOrphans.begin(), NotOrphans.end(), Attachments[i]) == NotOrphans.end()) {
string DeleteOrphaned = "del \"Prisegtukai\\" + Attachments[i] + "\"";
system(DeleteOrphaned.c_str());
}
}
system("rmdir /s /q cleanup");
}