Skip to content

Instantly share code, notes, and snippets.

@xtman
Created October 7, 2019 01:23
Show Gist options
  • Save xtman/f316008b325eb3413de3cb4bf845e5a5 to your computer and use it in GitHub Desktop.
Save xtman/f316008b325eb3413de3cb4bf845e5a5 to your computer and use it in GitHub Desktop.
Java app to search for illegal or problematic file names. The results are saved to a CSV file.
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicLong;
public class ScanForBadNames {
private static void printUsage() {
System.out.println();
System.out.println("Usage: s4bn <dir> <result.csv>");
System.out.println();
}
public static void main(String[] args) throws Throwable {
if (args == null || args.length < 1 || args.length > 2) {
System.err.println("Error: invalid arguments.");
printUsage();
System.exit(1);
}
Path root = Paths.get(args[0]);
if (!Files.exists(root)) {
System.err.println("Error: directory: '" + root + "' does not exist.");
printUsage();
System.exit(1);
}
if (!Files.isDirectory(root)) {
System.err.println("Error: '" + root + "' is not a directory.");
printUsage();
System.exit(1);
}
Path csv = Paths.get(args[1]);
AtomicLong nbDirsWithLeadingTrailingSpaces = new AtomicLong(0);
AtomicLong nbFilesWithLeadingTrailingSpaces = new AtomicLong(0);
AtomicLong nbDirsWithSpecialChars = new AtomicLong(0);
AtomicLong nbFilesWithSpecialChars = new AtomicLong(0);
AtomicLong nbRegularFiles = new AtomicLong(0);
AtomicLong nbSymbolicLinks = new AtomicLong(0); //
AtomicLong nbNonRegularFiles = new AtomicLong(0);
AtomicLong totalSize = new AtomicLong(0);
try (PrintStream ps = new PrintStream(new BufferedOutputStream(Files.newOutputStream(csv)))) {
ps.println("TYPE, PATH, LEADING/TRAILING SPACE, SPECIAL CHARACTER,");
ps.flush();
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (Files.isRegularFile(file)) {
nbRegularFiles.getAndIncrement();
totalSize.getAndAdd(Files.size(file));
} else {
if (Files.isSymbolicLink(file)) {
nbSymbolicLinks.getAndIncrement();
System.out.println("skipped symbolic link: \"" + file + "\"");
} else {
nbNonRegularFiles.getAndIncrement();
System.out.println("skipped non regular file: \"" + file + "\"");
}
return FileVisitResult.CONTINUE;
}
String fileName = file.getFileName().toString();
boolean leadingTrailingSpaces = fileName.startsWith(" ") || fileName.endsWith(" ");
if (leadingTrailingSpaces) {
nbFilesWithLeadingTrailingSpaces.getAndIncrement();
}
boolean specialChars = fileName.matches(".*[\\/:*?\"<>|].*");
if (specialChars) {
nbFilesWithSpecialChars.getAndIncrement();
}
if (leadingTrailingSpaces || specialChars) {
System.out.println("found bad file name: \"" + file.toString() + "\"");
String line = String.format("file,\"%s\",%s,%s,", root.relativize(file).toString(),
leadingTrailingSpaces ? "Y" : "", specialChars ? "Y" : "");
ps.println(line);
}
if (nbRegularFiles.get() % 1000 == 0) {
System.out.println("checked " + nbRegularFiles.get() + " files");
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException ioe) {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException ioe) {
String dirName = dir.getFileName().toString();
boolean leadingTrailingSpaces = dirName.startsWith(" ") || dirName.endsWith(" ");
if (leadingTrailingSpaces) {
nbDirsWithLeadingTrailingSpaces.getAndIncrement();
}
boolean specialChars = dirName.matches(".*[\\/:*?\"<>|].*");
if (specialChars) {
nbDirsWithSpecialChars.getAndIncrement();
}
if (leadingTrailingSpaces || specialChars) {
System.out.println("found bad directory name: \"" + dir.toString() + "\"");
String line = String.format("directory,\"%s\",%s,%s,", root.relativize(dir).toString(),
leadingTrailingSpaces ? "Y" : "", specialChars ? "Y" : "");
ps.println(line);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return super.preVisitDirectory(dir, attrs);
}
});
ps.println(",,,,");
ps.println(",,,,");
ps.println(",,,,");
ps.println(
String.format("Directory names contain special characters:,%d,,,", nbDirsWithSpecialChars.get()));
ps.println(String.format("Directory names have leading/trailing spaces:,%d,,,",
nbDirsWithLeadingTrailingSpaces.get()));
ps.println(String.format("File names contain special characters:,%d,,,", nbFilesWithSpecialChars.get()));
ps.println(String.format("File names have leading/trailing spaces:,%d,,,",
nbFilesWithLeadingTrailingSpaces.get()));
ps.println(String.format("Total number of regular files:,%d,,,", nbRegularFiles.get()));
if (nbSymbolicLinks.get() > 0) {
ps.println(String.format("Total number of symbolic links(skipped):,%d,,,", nbSymbolicLinks.get()));
}
if (nbNonRegularFiles.get() > 0) {
ps.println(String.format("Total number of non-regular files(skipped):,%d,,,", nbNonRegularFiles.get()));
}
ps.println(String.format("Total file size (bytes):,%d,,,", totalSize.get()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment