-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_dir.cpp
73 lines (68 loc) · 1.94 KB
/
delete_dir.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
/************************************************************************
** RollNo:2018201033 Name : Darshan Kansagara **
************************************************************************/
#include "myheader.h"
//**********************************************************************
// This function recursively remove all file and dir inside it.
// and finally delete dir itself
//**********************************************************************
void removeSingleDirectory(char *path)
{
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if( (string(dir->d_name) == "..") || (string(dir->d_name) == ".") )
{ }
else
{
string finalpath=string(path) + "/" +string(dir->d_name);
char* newpath = new char[finalpath.length() + 1];
strcpy(newpath, finalpath.c_str());
struct stat sb;
if (stat(newpath,&sb) == -1) {
perror("lstat");
}
else{
if((S_ISDIR(sb.st_mode)))
{
removeSingleDirectory(newpath);
}
else
{
removeSingleFile(newpath);
}
}
}
}
closedir(d);
int status= rmdir(path);
if(-1 == status)
{
showError("Error in removing the Directory with path ::::: "+string(path));
}
}
else{
showError("No such Directory Exist !!!");
}
}
//**********************************************************************
// It removes multiple directory that passed by User in argument
//**********************************************************************
void removeDirectories(vector<string> list)
{
if(list.size() < 2)
{
showError("Less number of argument in delete_dir !!!");
}
for(unsigned int i=1;i<list.size();i++)
{
char *path = new char[list[i].length() + 1];
strcpy(path, list[i].c_str());
//cout<<"\nrmdir path :"<<path<<endl;
removeSingleDirectory(path);
}
}