Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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
 

Monday, April 12, 2010

java setting multiple system properties via command line options

if you want to set system properties when you invoke java (vs setting them in the system before you start java/ command prompt) can do so with the -D option to java.exe or javaw.exe

You do this for each property.


Lets assume your command line to start jva initially looks like:
java org.mypack.Main

 So if you need to set to properties
usePort=2322
factoryClass=org.mypack.net.Factory1

Then the new command line will be:

java -DusePort=2322 -DfactoryClass=org.mypack.net.Factory1 org.mypack.Main

Pretty obvious except I was not sure if I need to put a ; between properties or have multiple -D

For those using an IDE:
See image - remember these are VM options (VM arguments) and not program arguments
(Click image for full size)

Tuesday, December 8, 2009

IE Default Print AND I love Java , PHP, VBA, mysql, J2ME, VB6, oracle, linux win 2000+

HEADER
&w&bPage &p of &P
FOOTER
&u&b&d





HEADER


&w&bPage &p of &P




FOOTER

&u&b&d




--------------------------



i love Java. Solve near every problem. Even UI with applets, J2ME and swing.



Java , J2ME mysql, apache web server - with them can solve most business problemns.



VBA - great tool - have delivered more than 10 projects using this language and it has helped with data and analysis with nearly all my other projects (in conjunction with Excel and Word).



VB6 - the full programming part of VBA



Windows 2000 was when windows OS got on par with unix and beat it in usability.



Oracle for showing all that a database can be.
MySql for being free and now powerful too.



J2ME for being easy to put apps on mobile devices



Linux for great performance ++ and all free. I use Ubuntu, burnt it on a CD from windows and made my system dual boot. instructions search



thank you all the folks behind these technologies!



Regards

Tushar Kapila


santaletternow.com   http://sel2in.com

http://stephenbrooks.org/muon1/ Volunteer computer idle time to science
www.thehungersite.com , and www.bhookh.com feed hungry , its free. www.ecologyfund.com
Please add these to your start-up how to: click here

http://www.worldcommunitygrid.org/ Volunteer computer idle time to help find cures for disease and other big world issues supported by IBM and other big corporates

Followers