import java.io.File;

/**
 * ChangeFileExtension - batch file extension changer.
 *
 * Copyright (C) 1999  Shazron Abdullah
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @author Shazron Abdullah
 * @since JDK1.1.7b
 * @version 1998
 */
public class ChangeFileExtension
{

public ChangeFileExtension(String directory, String oldextension, String newextension)
{
	File dirFile = new File(directory);
	if (! dirFile.exists())
	{
		System.out.println("Directory does not exist! : " + directory);
		System.exit(0);
	}

	if (! dirFile.isDirectory())
	{
		System.out.println(directory + " is not a directory!");
		System.exit(0);
	}

	String filesInDir[] = dirFile.list();

	if (filesInDir.length <= 0)
	{
		System.out.println(directory + " is empty.");
		System.exit(0);
	}

	// check for '.' in the extension
	if (newextension.indexOf(".") != 0)
		newextension ="." + newextension;

	for (int i = 0;  i < filesInDir.length; i++)
	{
		int index = filesInDir[i].indexOf(oldextension);

		if (index != -1) // the old extension exists in this file
		{
			String oldname = new StringBuffer(directory).append(File.separator)
			 .append(filesInDir[i]).toString();

			String newname = filesInDir[i].substring(0,index);
			newname = new StringBuffer(directory).append(File.separator)
			 .append(newname).append(newextension).toString();
			
			// now rename the file
			boolean success = new File(oldname).renameTo(new File(newname));
			if (! success)
				System.out.println("Rename failed!");

		}	
		
	}
	
}

public static void main(String args[])
{
	System.out.println(String.valueOf(args.length));
	if (args.length < 3)
		System.out.println("usage: ChangeFileExtension <directory> <oldextension> <newextension>");

	String directory = args[0];
	String oldextension = args[1];
	String newextension = args[2];

	new ChangeFileExtension(directory,oldextension, newextension);
}


};