renaming multiples files in a directory by removing similar charecters

Hi.

I have got a problem. I have got 100 mp3 files in a directory. All the mp3 file names start with E_. I want to programatically rename all these mp3 files by removing the charecters E_ from the file name. I used the below program but it is not working. Iam using java 1.6.

import java.io.File;

import java.io.IOException;

public class Ls {

public static void main(String argh_my_aching_fingers[]) {

String[] dir = new java.io.File("f:\\songs\\tar\\").list(); // Get list of names

java.util.Arrays.sort(dir); // Sort it (Data Structuring chapter))

for (int i = 0; i < dir.length; i++)

{

System.out.println(dir); // Print the list

String fileName = dir;

File f = new File("f:\\songs\\tar\\filename");

int length = fileName.length();

String newFileName=fileName.substring(2,length-4);

System.out.println("newFileName="+newFileName);

f.setWritable(true);

System.out.println("can be renamed"+f.canWrite());

System.out.println("the name change is"+f.renameTo(new File(newFileName)));

}

OUTPUT

- Run Java File -

E_ippud.mp3

newFileName=ippud

can be renamedfalse

the name change isfalse

Output completed (0 sec consumed) - Normal Termination

iam even setting the file property to writable. I dont know why it is not renaming the files.?

please help me. i

[1447 byte] By [bronze-starDukes] at [2007-11-26 12:13:02]
# 1

When you rename files I think that you have to give the full path for the new file name. Otherwise it will endup trying to move the file to the current working directory.

Files files[] = new File(dir).listFiles();

for (int i=0; i<files.length; i++){

File newFile = new File(files[i].getParentFile(), files[i].getName().substring(2));

files[i].setWritable(true);

files[i].renameTo(newFile);

}

>

silverstar at 2007-7-7 14:13:46 > top of Java-index,Archived Forums,Socket Programming...