'Having an issue with the metadata of my code repeating
package cmd;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class cmd {
static String pattern = "";
static String fileName = "";
static String Path = "";
public static void main(String[] args) throws IOException {
System.out.println("Please enter the filepath you would like to scan e.g. C,D,M");
Scanner s =new Scanner(System.in);
String filePath = s.next().toUpperCase()+":\\";
fileType();
walk(new File(filePath));
s.close();
}
public static void fileType() {
Scanner input = new Scanner(System.in);
int fileType;
System.out.println("Please select a filetype");
System.out.println("1 - .exe");
System.out.println("2 - .docx");
System.out.println("3 - .txt");
System.out.println("4 - .bat");
fileType = input.nextInt();
if(fileType>0 && fileType<5) {
switch(fileType) {
case 1:{
pattern = ".exe";
break;
}
case 2:{
pattern = ".docx";
break;
}
case 3:{
pattern = ".txt";
break;
}
case 4:{
pattern = ".bat";
break;
}
}
}
else {fileType();}
input.close();
}
public static void walk(File dir) throws IOException {
Path pathP = Paths.get(Path);
Path file =(pathP);
File listFile[]=dir.listFiles();
if (listFile !=null) {
for (int i=0;i<listFile.length;i++) {
if (listFile[i].isDirectory()) {
walk(listFile[i]);
}
else {
if (listFile[i].getName().endsWith(pattern)) {
BasicFileAttributes metadata = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println(listFile[i]+"Creation Time:"+metadata.creationTime() +" " +"Last Accessed: "+metadata.lastAccessTime()+" "+ "Last Modified: "+metadata.lastModifiedTime()+" " +"File Size:"+metadata.size());
}
}
}
}
}
}
I'm trying to print out the metadata for each individual document found during the walk but each line of metadata appears to be returning the exact same? I've attempted editing the basicfileattribtues to try and use the listFile[i] but when I do this I get a different error which I have no clue how to solve. After this issue I will be attempting to edit to print back to a .txt instead of the console.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
