IT Community - Software Programming, Web Development and Technical Support

How can I rename all files in a folder using Java?

This is a discussion on How can I rename all files in a folder using Java? within the Java Programming forums, part of the Software Development category; Hi All, Am trying to rename all the files in a given directory,for ex if theres a file called ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-11-2007, 07:15 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Question How can I rename all files in a folder using Java?

Hi All,
Am trying to rename all the files in a given directory,for ex if theres a file called hello.xls,hello1.xls then after the execution of the program it must become hello_Bs.xls,hello1_Bs.xls
I have written a simple code for this but am getting null pointer exception.
can anybody pls temme wats worng with the code.

Code:
import java.io.*;
import java.lang.*;
import java.util.*;

public class Ls {
	public static void main(String args[]) 
	{
		String[] dir = new java.io.File("c:\\Documents and Settings\\Admin\\Desktop\\New Folder").list();
		java.util.Arrays.sort(dir); 
		File f[] = new File("c:\\Documents and Settings\\Admin\\Desktop\\New Folder)").listFiles();
		int len1 = dir.length;
		for (int i=0;i<len1; i++)
		{
			System.out.println(dir[i]);
			String name = f[i].getName();
			System.out.println(name);
			int j=name.indexOf('.');
			System.out.println(j);
			String newname = name.substring(0,j-1) + "_BS" + name.substring(j+1,name.length());	
			System.out.println(newname);
			File newFileName=new File(f[i].getParentFile(), newname);
			System.out.println("newFileName="+newFileName);
			f[i].renameTo(newFileName);   
		}
	}	
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-11-2007, 11:34 PM
raja raja is offline
D-Web Trainee
 
Join Date: May 2007
Posts: 34
raja is on a distinguished road
Wink Re: How can I rename all files in a folder using Java?

Hi,

try this....

Code:
f[i].getName()
__________________
Raja. Myblog
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 12:09 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: How can I rename all files in a folder using Java?

The problem is here...

Code:
String newname = name.substring(0,j-1) + "_BS" + name.substring(j+1,name.length());
substring( a, b ) means including a excluding b

Fixed code is...

Code:
import java.io.File;

public class Ls {
	public static void main(String args[]) 
	{
		String[] dir = new java.io.File("test").list();
		java.util.Arrays.sort(dir); 
		File f[] = new File("test").listFiles();
		int len1 = dir.length;
		for (int i=0;i<len1; i++)
		{
			System.out.println(dir[i]);
			String name = f[i].getName();
			System.out.println(name);
			int j=name.indexOf('.');
			System.out.println(j);
			String newname = name.substring(0,j) + "_BS" + name.substring(j,name.length());	
			System.out.println(newname);
			File newFileName=new File(f[i].getParentFile(), newname);
			System.out.println("newFileName="+newFileName);
			f[i].renameTo(newFileName);   
		}
	}	
}
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 12:12 AM
raja raja is offline
D-Web Trainee
 
Join Date: May 2007
Posts: 34
raja is on a distinguished road
Wink Re: How can I rename all files in a folder using Java?

Oh...great!...I should of seen that.
__________________
Raja. Myblog
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-13-2007, 12:15 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default Re: How can I rename all files in a folder using Java?

Hi,

I tried this program but I am getting some problem in calling the "i" string. Could you please specify what "i" string is doing.

Thanks a lot.
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
create log files in java? saravanan Java Programming 2 08-06-2008 03:43 AM
To rename user created objects in sql server 2005 vadivelanshanmugam Database Support 3 03-06-2008 09:30 PM
Help me to rename the flex project seesamjagan Adobe Flex Programming 0 01-03-2008 10:55 PM
Rename Files in Bulk in Windows XP srikumar_l Operating Systems 0 12-19-2007 09:42 PM
How do I rename all the files from .htm to .html after copying them from a PC to a UN oxygen HTML, CSS and Javascript Coding Techniques 1 07-27-2007 04:04 AM


All times are GMT -7. The time now is 10:52 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0