Created
September 30, 2020 07:07
-
-
Save achimmihca/7ba87e9b99e2042141e797b7df092e1a to your computer and use it in GitHub Desktop.
Count files recursive. Show file count per extension for folder and sub-folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.HashMap; | |
import java.util.stream.Collectors; | |
/** | |
* Execute via `java --source 11 "CountFilesRecursive.java" "path/to/folder"` | |
*/ | |
public class CountFilesRecursive { | |
private static final String DEFAULT_FOLDER_PATH = "C:/Users/andi/Desktop"; | |
public static void main(String[] args) { | |
if (args.length < 1) | |
{ | |
args = new String[] { DEFAULT_FOLDER_PATH }; | |
} | |
File folder = new File(args[0]); | |
if(!folder.exists()) | |
{ | |
throw new RuntimeException("Folder does not exist: "+ args[0]); | |
} | |
HashMap<String, Integer> fileExtensionToCount = countFilesRecursive(folder); | |
printFileCount(folder, fileExtensionToCount); | |
} | |
private static void printFileCount(File file, HashMap<String, Integer> fileExtensionToCount) | |
{ | |
String fileExtensionAndCountCsv = fileExtensionToCount.entrySet().stream() | |
.map(entry -> entry.getKey() + ": "+ entry.getValue()) | |
.collect(Collectors.joining(", ")); | |
System.out.println(file.getAbsolutePath() + ": " + fileExtensionAndCountCsv); | |
} | |
private static HashMap<String, Integer> countFilesRecursive(File file) | |
{ | |
HashMap<String, Integer> fileExtensionToCount = new HashMap<>(); | |
for(File subFile : file.listFiles()) | |
{ | |
if(subFile.isDirectory()) | |
{ | |
HashMap<String, Integer> subFileExtensionToCount = countFilesRecursive(subFile); | |
printFileCount(subFile, subFileExtensionToCount); | |
mergeHashMaps(subFileExtensionToCount, fileExtensionToCount); | |
} | |
else if(subFile.isFile()) | |
{ | |
String extension = getFileExtension(subFile.getName()); | |
Integer count = fileExtensionToCount.get(extension); | |
if (count == null) | |
{ | |
count = 0; | |
} | |
count++; | |
fileExtensionToCount.put(extension, count); | |
} | |
} | |
return fileExtensionToCount; | |
} | |
private static String getFileExtension(String filePath) | |
{ | |
int i = filePath.indexOf("."); | |
if (i >= 0) | |
{ | |
return filePath.substring(i); | |
} | |
return ""; | |
} | |
private static void mergeHashMaps(HashMap<String, Integer> from, HashMap<String, Integer> to) | |
{ | |
for(var fromEntry : from.entrySet()) | |
{ | |
Integer toValue = to.get(fromEntry.getKey()); | |
if (toValue == null) | |
{ | |
to.put(fromEntry.getKey(), fromEntry.getValue()); | |
} | |
else | |
{ | |
Integer newToValue = toValue + fromEntry.getValue(); | |
to.put(fromEntry.getKey(), newToValue); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment