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  
Old 10-11-2007, 06:15 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
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
  #2  
Old 10-11-2007, 10: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  
Old 11-12-2007, 11:09 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 905
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  
Old 11-12-2007, 11:12 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?

Oh...great!...I should of seen that.
__________________
Raja. Myblog
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 11-12-2007, 11:15 PM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 319
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 Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Workgroup rename in Server2003 bluesky Server Management 8 10-01-2009 12:43 AM
create log files in java? saravanan Java Programming 2 08-06-2008 02:43 AM
Help me to rename the flex project seesamjagan Adobe Flex Programming 0 01-03-2008 09:55 PM
Rename Files in Bulk in Windows XP srikumar_l Operating Systems 0 12-19-2007 08: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 03:04 AM


All times are GMT -7. The time now is 11:48 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0