/* Albert Cardona 2007 This work is in the Public Domain. USE AT YOUR OWN RISK. */ import java.io.*; import ij.plugin.PlugIn; import ij.gui.GenericDialog; import ij.io.DirectoryChooser; import ij.IJ; /** The program takes: - a string with which to select a subset of files. - a string to look for in the subset of selected files. - a replacement string. If a file with the new name exists already, the renaming will not happen for that file and a warning is issued to the log window and the terminal. */ public class Rename_Files implements PlugIn { public void run(String arg) { DirectoryChooser dc = new DirectoryChooser("Select directory"); String dir = dc.getDirectory(); if (null == dir || dir.startsWith("null")) return; GenericDialog gd = new GenericDialog("Renamer"); gd.addMessage("Chosen dir:"); gd.addMessage(dir); gd.addStringField("File name contains: ", ""); gd.addStringField("Substring to replace: ", ""); gd.addStringField("Replacement substring: ", ""); gd.showDialog(); if (gd.wasCanceled()) return; // test: //IJ.log("_" + dir + "_\n_" + gd.getNextString() + "_\n_" + gd.getNextString() + "_\n_" + gd.getNextString() + "_"); main(new String[]{dir, gd.getNextString(), gd.getNextString(), gd.getNextString()}); } static public void main(String[] args) { if (args.length < 4) { p("Usage: java Rename_Files "); return; } String sdir = args[0]; String filter = args[1]; String A = args[2]; String B = args[3]; if (null == args[0] || null == args[2] || null == args[3] || args[0].equals("null") || args[2].equals("null") || args[3].equals("null")) { p("Improper arguments"); return; } File dir = new File(sdir); if (!dir.exists()) { p("Can't find directory " + sdir); return; } boolean use_filter = false; if (null != filter && filter.length() > 0) use_filter = true; try { String[] names = dir.list(); for (int i=0; i