Skip to content

Instantly share code, notes, and snippets.

@iolo
Last active June 20, 2024 05:56
Show Gist options
  • Save iolo/9e93082cd78b9ec3d948ffd43ec32c8d to your computer and use it in GitHub Desktop.
Save iolo/9e93082cd78b9ec3d948ffd43ec32c8d to your computer and use it in GitHub Desktop.
맥에서 만들어져서 풀어쓰기(NFD) 파일 이름을 모아쓰기(NFC) 파일 이름으로 변경(java버전)
import java.io.File;
import java.text.Normalizer;
import java.util.Set;
public class NFC {
public static void scanFiles(File dir, boolean recursive, boolean dryRun) {
for (File file: dir.listFiles()) {
var oldName = file.getName();
var newName = Normalizer.normalize(file.getName(), Normalizer.Form.NFC);
if (!oldName.equals(newName)) {
System.out.println(file.getPath() + " -> " + newName);
if (!dryRun) {
file.renameTo(new File(dir, newName));
}
}
if (recursive && file.isDirectory() && file.canRead()) {
scanFiles(file, true, dryRun);
}
}
}
public static void main(String ...args) {
Set<String> argSet = Set.of(args);
File baseDir = new File(argSet.stream().filter(it->!it.startsWith("-")).findFirst().orElse("."));
boolean dryRun = argSet.contains("--dry-run") || argSet.contains("-n");
boolean recursive = argSet.contains("--recursive") || argSet.contains("-r");
NFC.scanFiles(baseDir, recursive, dryRun);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment