-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRm.java
More file actions
104 lines (92 loc) · 2.86 KB
/
Copy pathRm.java
File metadata and controls
104 lines (92 loc) · 2.86 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package apppac;
import java.io.File;
import javax.swing.JOptionPane;
/**
*
* @author Shinigami
*/
public class Rm extends Commands
{
Search search=new Search();
void rm(String Path,String name) //name is full command begin with rm
{
if("rm ".compareTo(name.substring(0,3))==0)
{
String str=name.substring(name.indexOf(" ")+1);
if(search.search(Path,str))
{
delete(str,Path);
}
else
{
JOptionPane.showMessageDialog(null,"File does not exist");
}
}
else if("rmdir -r ".compareTo(name.substring(0,9))==0)
{
String str=name.substring(name.indexOf("-")+3); //str is name of directory
if(search.search(Path, str))
{
remove(str,Path);
delete(str,Path);
}
else
{
JOptionPane.showMessageDialog(null,"File does not exist");
}
}
else if("rmdir ".compareTo(name.substring(0,6))==0) //to delete directory
{
String str=name.substring(name.indexOf(" ")+1);
if(search.search(Path,str))
{
delete(str,Path);
}
else
{
JOptionPane.showMessageDialog(null,"File does not exist");
}
}
else
{
JOptionPane.showMessageDialog(null,"Command not found");
}
}
void delete(String str,String Path) //method to delete directiry or file
{
File file=new File(Path+"\\"+str);
if(file.delete())
{
JOptionPane.showMessageDialog(null,str+" delete successfully");
}
else
{
JOptionPane.showMessageDialog(null,str+" delete unsuccessfully");
}
}
void remove(String str,String Path)
{
String newPath=Path+"//"+str;
File f=new File(newPath);
File[] files=f.listFiles();
if(files.length==0)
return;
for(File file:files)
{
if(file.isDirectory())
{
remove(file.getName(),newPath);
delete(file.getName(),newPath);
}
else
{
delete(file.getName(),newPath);
}
}
}
}