Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in renaming a file with timestamp

 
gaurav kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i m trying to rename a text file with the current timestamp appended to its previous name. But the 'toRename' method which i m using is failing to do so. Though the same file is being renamed with some other String value.
The code i m using is as under:

File file = new File("D:\\project\\vision\\files");
String [] flName = file.list();
//for each order file in the directory
for(int i=0;i<flName.length;i++)
{
System.out.println("****filename****"+flName[i]);
File file1 = new File("D:\\project\\vision\\files"+"\\"+flName[i]);
Timestamp tmstamp = new Timestamp(System.currentTimeMillis());

boolean bool = file1.renameTo(new File("D:\\project\\vision\\files"+"\\"+tmstamp.toString));
System.out.println("****"+bool);
}
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the file name that you are proposing to use for the new name?
 
gaurav kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just want to append the current timestamp value to file name. For that i m using the 'toString' method of the timestamp but it doesn't seems to be working.
 
Hanuma Deepak Muvvala
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
it might be the case such that the toString method of timestamp will generate special characters like : which the file name wont accept in windows os ,so replace all special characters in that with some _ and rename ,then your problem may be solved.
 
Aalok Pandit
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gaurav,

as already pointed out, the problem is of the character ':' which is not acceptable in windows. the code works fine on Unix.

also, you might want to change the line
boolean bool = file1.renameTo(new File("D:\\project\\vision\\files"+"\\"+tmstamp.toString));

to
otherwise you may end up giving the same name to all files.

[ July 18, 2006: Message edited by: Aalok Pandit ]
[ July 18, 2006: Message edited by: Aalok Pandit ]
 
gaurav kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a ton!!! it solved my problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic