Assignment A Recursive Directory Search Utility for Windows This assignment comb
ID: 3842822 • Letter: A
Question
Assignment A Recursive Directory Search Utility for Windows This assignment combines recursion, looping and advanced file handling in a practical application for the Windows operating system's file structure. 60+ points See Canvas fordue date This assignment asks you to create a Java program that brings recursion, looping, file handling techniques and (optionally) advanced file date processing together in a useful Windows application. Your program will prompt the user for search criteria and will then 'crawl through a directory together with its subdirectories looking for files that qualify. Results of the search are displayed. Your program should search for files: By name (without an extension -e.g. MyTextFile) By extension name (e.g. java) By content (e.g. Math.sqrt) By modification date (optional extra credit-see below) Notes: The above criteria for searching may be combined in any form (see example runs below.) A valid initial path will always be entered other criteria are optional. You should allow the user to find file name entries regardless of upper/ower case characters in the file and extension names. Searching for content, however, should be case sensitive. The count of qualifying files should be displayed (see example output.) Your class (and your corresponding java file) should be named as follows: o MySearchExplanation / Answer
import java.io.File;
import java.util.Scanner;
import java.util.Calendar;
import java.util.Date;
public class MySearch {
public static void main(String[] argv) throws Exception {
String path;
String name;
String ext;
String con;
String date;
ArrayList<String> result = new ArrayList<String>();
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Directory search by path, name, extension, content and date"
System.out.println();
System.out.print("Enter starting directory for the search (like c: emp):");
path = sc.next();
System.out.print("Enter the file name like myfile or enter for all:");
name = sc.next();
System.out.print("Enter the file extension like txt or enter for all:");
ext = sc.next();
System.out.print("Enter the content to serach like csc211 or enter for all:");
con = sc.next();
System.out.print("Enter the last modified date like 11/12/2017 or enter for all:");
con = sc.next();
search(new File(path), name, ext, con, date, result);
System.out.println("Results - " + result.size() + " entries found");
for (i = 0; i< result.size(); i++){
System.out.println(result.get(i));
}
}
public static void serach(File dir, String name, String ext, String con, String date, ArrayList<String> list) {
int count;
System.out.println(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
search(new File(dir, children[i]), name, ext, con, date, list);
}
}
else {
count = 0;
if (name.length() ! = 0){
if (dir.getName() == name)
count = 1;
else
count = 0;
}
if (ext.length() ! = 0){
if (dir.getName().endsWith(ext))
count = 1;
else
count = 0;
}
if (con.length() ! = 0){
count = 0;
Scanner sc1 = new Scanner(dir);
String line;
while (sc1.hasNextLine()){
line = sc1.readLine();
if (line.contains(con)){
count = 1;
break;
}
}
}
if (date.length() ! = 0){
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
long milliSeconds = dir.lastModified();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
if (formatter.format(calendar.getTime()) == date)
count = 1
else
count = 0;
}
if (count == 1){
list.add(dir.getPath());
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.