-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletedir.cpp
72 lines (70 loc) · 1.87 KB
/
deletedir.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
//2018201005 Vatsal Soni
#include "config.h"
vector<string> deletefilesfolders;
vector<string> deleteDir(vector<string> &commandSplit) // It accepts arg from commandFile and call deleteall() for del.
{
int status;
char *remove_argument;
deletefilesfolders.clear();
for(unsigned i=1;i<commandSplit.size();i++)
{
remove_argument= new char[commandSplit[i].length()+1];
strcpy(remove_argument,commandSplit[i].c_str());
DIR *d;
d=opendir(remove_argument);
if(d)
{
status = rmdir(remove_argument);
if(status!=0)
{
deletefilesfolders.push_back(commandSplit[i]);
deleteall(commandSplit[i]);
}
}
else
cout<<endl<<"No such directory exist"<<endl;
}
/*for(unsigned i=0;i<deletefilesfolders.size();i++)
{
cout<<"***"<<deletefilesfolders[i]<<"***"<<endl;
}
char x;
cin>>x;*/
return deletefilesfolders;
}
void deleteall(string rm) // It recursively go till depth and delete all files/directories
{
struct dirent **namelist;
int i;
struct stat statObj;
string path;
char *rm1=new char[rm.length()+1];
strcpy(rm1,rm.c_str());
char *rm2;
int total = scandir(rm1,&namelist, NULL,alphasort);
for(i=0;i<total;i++)
{
path=rm+"/"+namelist[i]->d_name;
rm2=new char[path.length()+1];
strcpy(rm2,path.c_str());
//stat(rm2,&statObj);
if(stat(rm2,&statObj) < 0)
{
cout<<endl<<"No such directory exist"<<endl;
//return 1;
}
if(S_ISDIR(statObj.st_mode) && string(namelist[i]->d_name)!="." && string(namelist[i]->d_name)!="..")
{
//int f=current.find_last_of("/\\");
//current=current.substr(0,f);
//path=rm+"/"+namelist[i]->d_name;
deletefilesfolders.push_back(path);
deleteall(path);
}
else
{
if(string(namelist[i]->d_name)!="." && string(namelist[i]->d_name)!="..")
deletefilesfolders.push_back(path);
}
}
}