-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryOperations.cpp
83 lines (78 loc) · 1.88 KB
/
DirectoryOperations.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
#include"headers.h"
using namespace std;
//vector<string> dir_ent;
int tot_file;
int directorylist(const char *dir_name=NULL)
{
int count = 0;
dir_ent.clear();
DIR *dir=NULL;
struct dirent *entry=NULL;
string path;
dir_ent.resize(0);
dir = opendir(dir_name);
root=string(dir_name)+"/";
if(!dir)
{
cout<<"directory not found\n"<<dir_name<<endl;
return 0;
}
else
{
entry = readdir(dir);
while(entry != NULL)
{
if (entry->d_name[0]!='.')
{
if(string(dir_name).compare("/")!=0)
path=string(dir_name)+"/"+string(entry->d_name);
else
path="/"+string(entry->d_name);
//root=path;
dir_ent.push_back(string(path));
count++;
}
entry = readdir(dir);
}
}
closedir(dir);
return count;
}
void PrintFileProperties(struct stat stats)
{
clrscr();
struct tm dt;
cout<<CYAN<<"File access: "<<RESET;
if (stats.st_mode & R_OK)
cout<<CYAN<<"read "<<RESET;
if (stats.st_mode & W_OK)
cout<<CYAN<<"write "<<RESET;
if (stats.st_mode & X_OK)
cout<<CYAN"execute"<<RESET;
cout<<CYAN<<"\nFile size: "<<stats.st_size<<"kb"<<RESET;
dt = *(gmtime(&stats.st_ctime));
cout<<CYAN<<"\nCreated on: "<<dt.tm_mday<<"-"<<dt.tm_mon<<"-"<<dt.tm_year + 1900<<
" "<<dt.tm_hour<<":"<<dt.tm_min<<":"<<dt.tm_sec<<RESET;
dt = *(gmtime(&stats.st_mtime));
cout<<CYAN<<"\nModified on: "<<dt.tm_mday<<"-"<<dt.tm_mon<<"-"<<dt.tm_year + 1900<<
" "<<dt.tm_hour<<":"<<dt.tm_min<<":"<<dt.tm_sec<<RESET;
}
void ExploreDirectory(const char *dir_name=NULL)
{
clrscr();
struct stat info;
tot_file=directorylist(dir_name);
for(int i=0;i<tot_file;i++)
{
string t = dir_ent[i];
stat(t.c_str(),&info);
if(S_ISDIR(info.st_mode))
{
cout<<GREEN<<t<<RESET<<endl;
}
else
{
cout<<RED<<t<<RESET<<endl;
}
}
}