I need to move all files ending with .txt to the archive folder.
I have the below code, but the if (sourcepath.endsWith(".txt"))
doesn't validate the file extension.
File directory = new File("Archive");
System.out.println((System.getProperty("user.dir")));
File directory1 = new File (System.getProperty("user.dir"));
File[] files = directory1.listFiles();
if (! directory.exists()) {
directory.mkdir();
for(File f : files ) {
Path sourcePath = Paths.get(f.getName());
System.out.println(sourcePath);
if (sourcePath.endsWith(".txt")){ // Not validating
System.out.println(sourcePath.endsWith(extension));
Files.move(sourcePath, Paths.get("Archive"));
}
}
System.out.println("Clearing Folder.....All Files moved to Archive Directory");
}
Expected Output:
C:\Users\harsshah\workspace\FFPreBatchValidation
.classpath
.project
.settings
bin
kjnk.txt
src
kjnk.txt should be moved to Archive folder