I had client who was saving files across the network from a Solaris box. They wanted the file renamed but the Solaris box was very underpowered so I wrote a java find file and rename so that it work under windows.
//find file in java
//Mike Tremaine mgt@stellarcore.net
import java.io.*;
public class find {
public static String[] parseopts(String[] input_args) {
//Initalize Options
String[] options = new String[3];
for ( String myarg : input_args ) {
//Switch for each args - case does not allow strings
if ( myarg.startsWith("--path=") ) {
options[0] = myarg.substring(7,myarg.length());
options[0] = options[0].replaceAll("\"","");
} else if ( myarg.startsWith("--file=") ) {
options[1] = myarg.substring(7,myarg.length());
options[1] = options[1].replaceAll("\"","");
} else if ( myarg.startsWith("--newfile=") ) {
options[2] = myarg.substring(10,myarg.length());
options[2] = options[2].replaceAll("\"","");
}
}
//Check that we have path file and newfile
if ( options[0] == null || options[1] == null || options[2] == null ) {
System.err.println( "Usage: find [--path=path to search] [--file=filename to find] [--newfile=suffix to append]");
System.exit(1);
}
// Else go ahead
return options;
}
private static void searchpath (String i_path, String i_file, String n_suffix) throws IOException {
File m_dir = new File (i_path);
File [] m_files = m_dir.listFiles();
//For loop search/rename files and recurse
for ( File m_file : m_files ) {
if ( m_file.isFile() && i_file.equals(m_file.getName()) ) {
//Before we add to array increment counter and check array size
System.out.println("File found: " + m_file.getAbsolutePath() );
File newfile = new File (m_file.getAbsolutePath() + "." + n_suffix);
//Rename
boolean success = m_file.renameTo(newfile);
if (!success) {
System.out.println("File rename failed: " + newfile.getAbsolutePath() );
}
// Do not follow symlinks test absolute vs canonical dir names
} else if ( m_file.isDirectory() && m_file.getAbsolutePath().equals(m_file.getCanonicalPath()) ) {
//Call searchpath again and recurse
//System.out.println("Debug in Absolute dir found: " + m_file.getAbsolutePath() );
try {
searchpath( m_file.getAbsolutePath(), i_file, n_suffix );
}
catch ( Exception e ) {
System.out.println("Error in searchpath check permissions: " + m_file );
}
}
}
}
public static void main (String[] args) {
//Parse the input Args
String[] input = parseopts(args);
//Start search/rename
try {
searchpath(input[0],input[1],input[2]);
}
catch ( Exception e ) {
System.out.println("Error in searchpath check permissions: " + input[0] );
}
}
}
// vi: shiftwidth=3 tabstop=3 et