Jump to content

Renaming A File


awitt
 Share

Recommended Posts

First off, I know this is a javascript forum, not a Java forum, however, I never seem to get any responses when I post on the Java forum. So, because of a deadline, I decided to post here in the hopes that someone may be able to help.Does anyone see a reason this won't work?

import java.io.File;public class scan{	public static void main(String[] args)	{		File myDir = new File("C:\\Documents and Settings\\vid\\Desktop\\testfolder");		String[] files = myDir.list();		final int maxLength = 35;		int incrementalNumber = 0;		        //for (String file : files)        for (int i = 0; i < files.length; i++)        {        	String fileName = files[i].toString();System.out.println("fileName: " + fileName);        	        	if(fileName.length() > maxLength)        	{        		File oldfile = new File(fileName);        		        		String newFileName = fileName.substring(0, 28);        		String extension = fileName.substring(fileName.length() - 4,         				fileName.length());        		newFileName += incrementalNumber + extension;System.out.println("newFileName: " + newFileName);        		        		File newFile = new File(newFileName);        		System.out.println( oldfile.renameTo(newFile) ); // **returns false every time**        		incrementalNumber++;        	}			System.out.println("---------------------------");        }	}}

All the files are readable and there are no other files in that directory with the same name as the new one. I don't know why or how this is not working, and I do not know how I could go about debugging or testing to find out either. Any help would be much appreciated.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...