-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.cpp
82 lines (58 loc) · 2.52 KB
/
FileManager.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
#include "FileManager.h"
#include <string>
#include <dirent.h>
#include <sys/stat.h>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
// takes in a pointer to folderpath, meaning an address value of filepath
// whoever calls this method, will have to supply a pointer with no-&
// and will get back a string
vector<File> FileManager::listFilesInPath(const char *folderPath){
DIR *dir = opendir(folderPath);
struct dirent *entry;
vector<File> files;
while((entry = readdir(dir)) != nullptr){
if(entry->d_name[0] != '.'){
string fileName = string(entry->d_name);
string fullPath = string(folderPath) + fileName;
files.emplace_back(File(fileName, getLastOpenedTime(fullPath.c_str())));
}
}
return files;
}
// takes in a pointer to filepath, meaning an address value of filepath
// and returns an address value of char for getLasOpenedTime
// whoever calls this method, will have to supply a pointer
// and will get back a char pointer meaning that to access it i'll just need no &
// if I use * it will point to the first element only ---> per vid
string FileManager::getLastOpenedTime(const char *filePath){
struct stat info;
// here we're sending the address value of info because info is not a pointer
// and stat is expecting a pointer of char
stat(filePath, &info);
boost::posix_time::ptime fileLastOpenedPTime = boost::posix_time::from_time_t(info.st_atime);
std::string fileLastOpenedTime = boost::posix_time::to_simple_string(fileLastOpenedPTime);
return fileLastOpenedTime;
}
vector<File> FileManager::getOldFiles(string oldDate, vector<File> files){
// convert oldDate string to ptime
boost::posix_time::ptime oldDatePtime = boost::posix_time::time_from_string(oldDate);
// anything that is older than this date will be added to a vector
vector<File> oldFiles;
for(int i =0; i < files.size(); i++){
boost::posix_time::ptime lastOpenedPTime = boost::posix_time::time_from_string(files[i].getLastOpenedTime());
if(lastOpenedPTime < oldDatePtime){
oldFiles.push_back(files[i]);
}
}
return oldFiles;
}
int FileManager::moveFilesToFolder(vector<File> files, string folderPath){
int result = 0;
for(int i = 0; i < files.size(); i++){
string newPath = folderPath + files[i].getName();
string currentPath = "/home/fhm-capra/Desktop/" + files[i].getName();
result += rename(currentPath.c_str(), newPath.c_str());
}
return result;
}