Thursday, June 12, 2014

Java: rename file : file_0001.dat, file_0002 to file_0004, file_0008.dat

Issue: bunch of files name file_0001.dat, file_0002.dat......file_1000.dat.
 I want to change the file names such as the number after file_ will be a
 multiple of 4 in comparison to previous file name. So i want to change 

file_0001.dat to file_0004.dat
file_0002.dat to file_0008.dat 
 
Start from the highest and go to lowest. As 
if u go from lowest - there will be conflicts. example u cant rename 001
 to 004 if there is a 004 from before ...what if it at first you have 
file_0001.dat, file_0001.dat, ... file_0004.dat. 
  
 import java.util.*;
import java.io.*;

public class RenameFiles{
    private Properties props = new Properties();
    private String args[];
    private TreeMap<Integer, FileRen> ffs = new TreeMap<Integer, FileRen>();

    RenameFiles(String args[]){
        this.args = args;
        start();
    }
    public static void main(String args[]){
        new RenameFiles(args);
    }

    void start(){
        defaults();
        load();
        scanDir();
        renameDo();

    }

    void renameDo(){
        final int ll = ffs.size();
        if(ll < 1){
            System.err.println( "Nothing to do ffs size " + ll );
            return;
        }
        ArrayList<FileRen>  frs = new ArrayList<FileRen>();
        frs.addAll(ffs.values());
        for(int i  = ll - 1;i >= 0; i--){
            FileRen fr = frs.get(i);
            boolean t = fr.orig.renameTo(fr.renamed);
            System.err.println(i + " Result " + t + " " + fr.orig + " to " +  fr.renamed);
        }
    }

    void load(){
    if(args.length > 1){
        }
    }

    void defaults(){
        props.setProperty("dir", ".");
    }

    void scanDir(){
        File f = new File(props.getProperty("dir"));
        FFilter ffilter = new FFilter();
        String []l = f.list(ffilter);
        for(String nn : l){
            FileRen fr = FileRen.check(f, nn);
            if(fr != null){
                ffs.put(fr.idx, fr);
            }
        }
    }
}

    class FFilter implements java.io.FilenameFilter
    {
    public boolean accept(File dir,
             String name){
        System.err.println(dir + " " + name + " check ");
        File f = new File(dir, name);
        if(!f.exists() || !f.isFile())return false;
        final int i = name.lastIndexOf(".");
        final int i2 = name.lastIndexOf("_");
        if(i2 < 0 || i < i2)return false;
        return true;
        }
    }

class FileRen{
    File orig;
    File renamed;
    int idx;
        FileRen(){

        }

        static FileRen check(File parent, String s){
            final int i = s.lastIndexOf(".");
            final int i2 = s.lastIndexOf("_");
            //file_0001.dat to file_0004.dat
            if(i2 < 0 || i < i2)return null;
            String p = s.substring(i2+1, i);
            try{
                int idx = Integer.parseInt(p);
                if(idx > 0){
                    FileRen f = new FileRen();
                    f.orig = new File(parent, s);
                    f.idx = idx;
                    int nxt = idx * 4;
                    String pad = pad(nxt, 4);
                    f.renamed = new File(parent, s.substring(0, i2 + 1) + pad +s.substring(i) );
                    return f;
                }
            }catch(Exception e){
                System.err.println(e + " " + parent + " " + s);
            }
            return null;
        }

        static String pad(int i, int k){
            StringBuilder sb = new StringBuilder().append(i);
            int ll = sb.length();
            for(int c = ll; c <= k; c++){
                sb.insert(0, '0');
            }
            return sb.toString();
        }
    }
 
 
Utility script to make a few files, test helper
touch file_0001.dat touch file_0002.dat touch file_0004.dat touch file_0005.dat touch file_0007.dat This version just start the program in the same dir as the files, after compiling and changing the classpath
javac /prog/j/apps/sel2in/ioFiles/RenameFiles.java
java -cp /prog/j/apps/sel2in/ioFiles/ RenameFiles
 

No comments:

Post a Comment

Followers