001 package com.croftsoft.apps.rename;
002
003 import java.io.*;
004 import java.util.*;
005
006 /*********************************************************************
007 * Renames files in a directory.
008 *
009 * <P>
010 *
011 * @version
012 * 2000-05-09
013 * @author
014 * <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A>
015 *********************************************************************/
016
017 public class Rename implements FilenameFilter
018 //////////////////////////////////////////////////////////////////////
019 //////////////////////////////////////////////////////////////////////
020 {
021
022 private final String oldNameTemplate;
023
024 private final String pre;
025
026 private final String post;
027
028 //////////////////////////////////////////////////////////////////////
029 //////////////////////////////////////////////////////////////////////
030
031 public static void main ( String [ ] args ) throws Exception
032 //////////////////////////////////////////////////////////////////////
033 {
034 rename ( new File ( "." ), args [ 0 ], args [ 1 ] );
035 }
036
037 public static void rename (
038 File directory,
039 String oldNameTemplate,
040 String newNameTemplate )
041 //////////////////////////////////////////////////////////////////////
042 {
043 if ( directory == null )
044 {
045 throw new IllegalArgumentException ( "null directory" );
046 }
047
048 FilenameFilter filenameFilter = new Rename ( oldNameTemplate );
049
050 File [ ] files = directory.listFiles ( filenameFilter );
051
052 for ( int i = 0; i < files.length; i++ )
053 {
054 String oldName = files [ i ].getName ( );
055
056 File newFile = new File ( directory,
057 toNewName ( oldName, oldNameTemplate, newNameTemplate ) );
058
059 if ( !files [ i ].renameTo ( newFile ) )
060 {
061 throw new RuntimeException ( "Failure renaming \""
062 + files [ i ] + "\" to \"" + newFile + "\"" );
063 }
064 }
065 }
066
067 public static String toNewName (
068 String oldName,
069 String oldNameTemplate,
070 String newNameTemplate )
071 //////////////////////////////////////////////////////////////////////
072 {
073 String [ ] oldParts = parseParts ( oldNameTemplate );
074
075 String value = oldName.substring ( oldParts [ 0 ].length ( ),
076 oldName.length ( ) - oldParts [ 1 ].length ( ) );
077
078 String [ ] newParts = parseParts ( newNameTemplate );
079
080 return newParts [ 0 ] + value + newParts [ 1 ];
081 }
082
083 //////////////////////////////////////////////////////////////////////
084 //////////////////////////////////////////////////////////////////////
085
086 public Rename (
087 String oldNameTemplate )
088 //////////////////////////////////////////////////////////////////////
089 {
090 this.oldNameTemplate = oldNameTemplate;
091
092 String [ ] parts = parseParts ( oldNameTemplate );
093
094 pre = parts [ 0 ];
095
096 post = parts [ 1 ];
097 }
098
099 //////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////
101
102 public boolean accept (
103 File directory,
104 String filename )
105 //////////////////////////////////////////////////////////////////////
106 {
107 return filename.startsWith ( pre )
108 && filename.endsWith ( post );
109 }
110
111 //////////////////////////////////////////////////////////////////////
112 // private methods
113 //////////////////////////////////////////////////////////////////////
114
115 private static String [ ] parseParts ( String filenameTemplate )
116 //////////////////////////////////////////////////////////////////////
117 {
118 int index = filenameTemplate.indexOf ( '*' );
119
120 if ( index < 0 )
121 {
122 throw new IllegalArgumentException (
123 "No wildcard character (*) in \"" + filenameTemplate + "\"" );
124 }
125
126 String pre = filenameTemplate.substring ( 0, index );
127
128 String post = filenameTemplate.substring ( index + 1 );
129
130 return new String [ ] { pre, post };
131 }
132
133 //////////////////////////////////////////////////////////////////////
134 //////////////////////////////////////////////////////////////////////
135 }