-
Notifications
You must be signed in to change notification settings - Fork 2
/
Crawler.java
141 lines (127 loc) · 3.72 KB
/
Crawler.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.Scanner;
public class Crawler{
private String _tempPath = "";
private int _numFiles = 0;
private int _numFolders = 0;
private ArrayList<String> _fileList = new ArrayList<String>();
private ArrayList<String> _folderList = new ArrayList<String>();
private String _choice;
private int _add;
public Crawler(){
_numFiles = 0;
_numFolders = 0;
_fileList.clear();
_folderList.clear();
_add = 0;
}
public void reset(){
_numFiles = 0;
_numFolders = 0;
_fileList.clear();
_folderList.clear();
_add = 0;
}
public void crawl(String path, boolean mod){
File start = new File(path);
File[] fList = start.listFiles();
for (File file : fList){
if (file.isFile()){
if(mod){
System.out.println(file.getAbsolutePath());
}
this._numFiles++;
}
else if (file.isDirectory()){
this._folderList.add(this._numFolders, file.getAbsolutePath());
this._numFolders++;
this._tempPath = file.getAbsolutePath();
crawl(this._tempPath, mod);
}
if(mod){
System.out.println(file.getName());
}
}
}
public ArrayList<String> searchExt(String ext, String root){
File start = new File(root);
File[] fList = start.listFiles();
for (File file : fList){
if (file.isFile() && file.getAbsolutePath().contains(ext)){
_fileList.add(_numFiles, file.getAbsolutePath());
_numFiles++;
}
else if (file.isDirectory()){
_numFolders++;
_tempPath = file.getAbsolutePath();
String ext1 = ext;
searchExt(ext1 , _tempPath);
}
}
return _fileList;
}
public int getSize(String root){///////////////////////////
int size = 0;
return size;
}
public void delete(ArrayList<String> list, Boolean mod){
Scanner sc = new Scanner(System.in);
if(!mod){
System.out.println("YOU ARE ABOUT TO DELETE " + list.size() + " FILES! ARE YOU SURE? [dY/N]");
_choice = sc.nextLine();
}
else if(mod && _add == 0){
System.out.println("YOU ARE ABOUT TO DELETE " + list.size() + " FILES AND " + (_numFolders + 1) + " FOLDERS ARE YOU SURE? [Y/N]");
_choice = sc.nextLine();
_choice = _choice.toUpperCase();
}
if(((_choice.equals("Y")))){
while( list.size() > 0){
this._numFiles = list.size() - 1;
String path = list.get(this._numFiles);
path.replaceAll("\\\\", "\\\\\\\\");
System.err.println("DELETING FILE [ " + list.get(this._numFiles) + " ]");
list.remove(this._numFiles);
File temp = new File(path);
temp.delete();
}
if(mod && _add == 1){
while(_folderList.size() > 0){
this._numFolders = this._folderList.size() - 1;
String path = this._folderList.get(_numFolders);
path.replaceAll("\\\\", "\\\\\\\\");
System.err.println("DELETING FOLDER [ " + this._folderList.get(this._numFolders) + " ]");
this._folderList.remove(this._numFolders);
File temp = new File(path);
temp.delete();
}
}
}
else{
System.err.println("OPERATION ABORTED");
}
}
public void displayList(ArrayList<String> list){
while(list.size() > 0){
System.out.println(list.size() + ": [ " + list.get(list.size() - 1) + " ]");
list.remove(list.size() - 1);
}
}
public void deleteDirectory(String root){
ArrayList<String> tempList = new ArrayList<String>();
tempList = this.searchExt(".",root);
this.delete(tempList,true);
this.reset();
this.crawl(root, false);
this._add = this._add + 1;
this.delete(tempList,true);
this.reset();
File temp1 = new File(root);
temp1.delete();
}
}