Skip to content

Instantly share code, notes, and snippets.

@doct0rX
Last active March 19, 2019 15:55
Show Gist options
  • Save doct0rX/a2bb132244d2190aa299529d166c5a37 to your computer and use it in GitHub Desktop.
Save doct0rX/a2bb132244d2190aa299529d166c5a37 to your computer and use it in GitHub Desktop.
Not to delete specified files -- how it work -> simply run it with Java and it'll guide you
/**
* Author: Mustafa Jamal
*
* This program for deleting not selected files.
*/
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.nio.file.Paths;
public class DontDeleteThoseFiles {
private static String[] pArgs;
private static String NOT_UPLOADED_TXT;
private static String DIR_PATH;
private static File DIR_ABSOLUTE_PATH;
private static List<File> DELETE_FILES_NAMES = new ArrayList<File>();
private static List<String> KEEP_RECORDS = new ArrayList<String>();
private static ArrayList<String> keptFiles = new ArrayList<String>();
private static String DIR_ABSOLUTE_PATH_STRING;
private static void errCheck1() {
if (pArgs.length != 2) {
System.err.println((char)27 +
"[31mAdd this file in the directory/Folder you gonna use to delete from... This will be fixed in up comming versions!\n" +
"you have to specify one .txt file and one Directory_Path for the app to run.\n e.g., $ java RemoveUploadedGooglePhotos [wanted_Files.txt] [Dir Path]\n" +
" ----------------------------------------------------------\n" +
"| java RemoveUploadedGooglePhotos.java wanted_Files.txt . |\n" +
" ----------------------------------------------------------" + (char)27 + "[0m");
System.exit(1);
}
}
private static void errCheck2() {
if (!(DIR_PATH.startsWith(".") || DIR_PATH.startsWith(System.getProperty("file.separator")))) {
System.err.println((char) 27 + "[31m\nYou MuST specify a valid path.\n (e.g., [.image] or [/image]\n"
+ (char) 27 + "[0m");
System.exit(2);
}
}
private static List<String> unUploadedFiles() {
try {
BufferedReader reader = new BufferedReader(new FileReader(NOT_UPLOADED_TXT));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("\n") || line.contains(" ")) {
KEEP_RECORDS.add(DIR_ABSOLUTE_PATH_STRING + line);
}
}
keptFiles.add("DontDeleteThoseFiles.java");
keptFiles.add("DontDeleteThoseFiles.class");
keptFiles.add(NOT_UPLOADED_TXT);
KEEP_RECORDS.addAll(keptFiles);
reader.close();
return KEEP_RECORDS;
} catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", NOT_UPLOADED_TXT);
e.printStackTrace();
return null;
}
}
private static void printUnuploadedFiles() {
System.out.println("\n" + (KEEP_RECORDS.size() - keptFiles.size()) + " Files in the: " + NOT_UPLOADED_TXT);
for (String file : KEEP_RECORDS) {
if (!keptFiles.contains(file)) {
System.out.println("\t" + file);
}
}
}
private static File[] directoryFileList() {
return DIR_ABSOLUTE_PATH.listFiles();
}
private static List<File> willDeleteFilesList() {
for (File file: directoryFileList()) {
if (!KEEP_RECORDS.contains(DIR_ABSOLUTE_PATH_STRING + file.getName().toString())) {
DELETE_FILES_NAMES.add(file);
}
}
return DELETE_FILES_NAMES;
}
private static void deleteFiles() {
if (!willDeleteFilesList().isEmpty()) {
System.out.println("\nDeleted Files:");
for (File file: DELETE_FILES_NAMES) {
System.out.println("\t" + file.getName());
file.delete();
}
} else {
System.out.println("\nNo Files To Delete\n");
}
}
public static void main(String[] args) {
pArgs = args;
errCheck1();
NOT_UPLOADED_TXT = args[0];
DIR_PATH = args[1];
errCheck2();
File file = new File(DIR_PATH);
DIR_ABSOLUTE_PATH = new File(file.getAbsolutePath());
DIR_ABSOLUTE_PATH_STRING = DIR_ABSOLUTE_PATH.getName()+ System.getProperty("file.separator");
KEEP_RECORDS = unUploadedFiles();
printUnuploadedFiles();
deleteFiles();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment