Android File Rename renameFiles(File file, String filterRegex, String replaceRegex, String replacement)

Here you can find the source of renameFiles(File file, String filterRegex, String replaceRegex, String replacement)

Description

recursively rename all sub files while matches filterRegex, replace each substring of filename that matches replaceRegex with the given replacement.

Parameter

Parameter Description
file a parameter
filterRegex a parameter
replaceRegex a parameter
replacement a parameter

Declaration

public static boolean renameFiles(File file, String filterRegex,
        String replaceRegex, String replacement) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**//ww  w  .ja v a  2 s  . c o  m
     * recursively rename all sub files while matches filterRegex, replace each
     * substring of filename that matches replaceRegex with the given
     * replacement.
     * @param file
     * @param filterRegex
     * @param replaceRegex
     * @param replacement
     * @return
     */
    public static boolean renameFiles(File file, String filterRegex,
            String replaceRegex, String replacement) {
        if (!file.exists())
            return true;
        // recursively traversal all sub files
        if (file.isDirectory()) {
            File[] subFiles = file.listFiles();
            for (File subFile : subFiles) {
                if (!renameFiles(subFile, filterRegex, replaceRegex,
                        replacement))
                    return false;
            }
        } else if (file.getName().matches(filterRegex)) {
            String newFileName = file.getName().replaceAll(replaceRegex,
                    replacement);
            String directory = file.getParent();
            return file.renameTo(new File(directory + File.separator
                    + newFileName));
        }
        return true;
    }

    public static boolean renameFiles(File file, String regex,
            String replacement) {
        return renameFiles(file, ".*", regex, replacement);
    }
}

Related

  1. renameAs(String pathDir, String name, String wishName)
  2. renameFile(File source, File dest)
  3. renameFile(String origPath, String destPath)
  4. renameFile(String source, String dest)
  5. renameFile(String strSrc, String strDest)
  6. renameFiles(File file, String regex, String replacement)
  7. renameFile(File file, File newFile, boolean isDeleteExistDestFile)