Rename of recurring files

Beside the paper, which you collect with your scanner, you get a lot of documents in a digital way, like bills or payrolls.

These documents, I would name recurring. They appear on a regular base in my infolder. But in the most cases, these documents do not follow my own defined filename format. I want the format as YYYY-MM-DD – author – description

So the first task would be, to create an easy and repeatable way, to rename these files automatically.

I define an example pattern, based on a bank statement from the ING DiBa (example filename: Girokonto_350238442_Kontoauszug_20120212.pdf). The related RegEx pattern is:

private String pattern = 

Important is the text between the ( ). Here we get parts, which we need for the new name, the values for year, month and day, because the wanted target filename is 2012-02-12 – ing diba – girokonto kontoauszug.pdf

private String replaceWith = 
  "$1-$2-$3 - ing diba - girokonto kontoauszug.pdf";

With „$1“ we get, what was between the first „()“, in this case \\d{4}, with the value 2012.

Put this together, to rename the file:

name.toString().replaceAll(pattern, replaceWith);

name in this case, if from datatype Path, which was added to Java in version 7.

Here a full example:

import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class RenameFiles {
  private Path rootPath;
  private String pattern = "Girokonto_350238442_Kontoauszug_(\\d{4})(\\d{2})(\\d{2}).pdf";
  private String replaceWith = "$1-$2-$3 - ing diba - girokonto kontoauszug.pdf";

  public RenameFiles() {
    rootPath = Paths.get(".");

    try {
      Finder finder = new Finder(pattern);
      Files.walkFileTree(rootPath, finder);
    } catch (IOException e) {

  class Finder extends SimpleFileVisitor {
    private final PathMatcher matcher;

    private Finder(String pattern) {
      // Here the PathMather pattern is defined
      // "regex:" defines the pattern
      matcher = FileSystems.getDefault().getPathMatcher("regex:" + pattern);

    private void find(Path file) {
      Path name = file.getFileName();
      if (name != null && matcher.matches(name)) {
        // Replace the old with the new filename
        String newName = name.toString().replaceAll(pattern, replaceWith);

        try {
          Files.move(file, rootPath.resolve(newName));
        } catch (Exception e) {

    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
      return FileVisitResult.CONTINUE;

  public static void main(String[] args) {
    new RenameFiles();

If you use the root folder as input for Files.walkFileTree, don’t be surprised, when it takes time, and of course it would be a good idea to start with a System.out.println ;-).
This is a very simple implementation, which can easily be enriched.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.