cancel
Showing results for 
Search instead for 
Did you mean: 

renameTo + tiimestamp

SOLVED
New Contributor

renameTo + tiimestamp

Hello everyone, I have this piece of groovy script that renames a file, this works fine as long as the name of the newfile doesn't change. However I wish to rename file = filename + timestamp (yyyy-mm-dd).xlsx . I have started creating a small timestamp to do this, however when I'm deperately trying to use this timestamp in my filename, but so far it does not work. Ny fiile is renamed java.text.SimpleDateFormat@69cbaae or Filename + Null or I get syntax errors.

 

I'm using V3.0 pro and xlsx format.

 

public class RenameFileJavaDemo
{
public static void main(String[] args)
{
//Old File
File oldfile =new File("C:/temp/ISPC-OUTPUT.xlsx");

//New File
// File newfile =new File("C:/temp/ISPC-OUTPUT_NEW1.xlsx");

File newfile =new File('C:/temp/ISPC-OUTPUT_NEW1' + '.xlsx');
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-M-yyyy a");
df.setTimeZone(TimeZone.getTimeZone("Europe/Oslo"));

def datetime = df.format(date)
/*renameTo() return boolean value
* It return true if rename operation is
* successful
*/
boolean flag = oldfile.renameTo(newfile);
if(flag){
System.out.println("File renamed successfully");
}else{
System.out.println("Rename operation failed");
}
}
}

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Community Hero

Re: renameTo + tiimestamp

Are you able to manually rename a file with a name that looks like what you want?  Perhaps it's a Windows format issue.

 

The other thing I would check is whether or not your date is actually a string.  It looks like one but it might not be and you may need to convert it to one before you can use it in a file name.

View solution in original post

Highlighted
New Contributor

Re: renameTo + tiimestamp

A co worker just found this solution for me that works, I owe him :-)

 

import groovy.io.FileType

def now = new Date()
def oldfile = new File("C:/temp/ISPC-OUTPUT.xlsx")

String empty = ""
oldfile.parentFile.eachFile (FileType.FILES)
{
file ->
curDate = now.format("yyyyMMdd", TimeZone.getTimeZone('UTC'))
String newName = file.path
newName = newName.replaceAll(~/ISPC-OUTPUT/, "ISPC-OUTPUT"+"_"+curDate)
file.renameTo newName
log.info "New Name:- "+newName
}

View solution in original post

3 REPLIES 3
Highlighted
Community Hero

Re: renameTo + tiimestamp

Are you able to manually rename a file with a name that looks like what you want?  Perhaps it's a Windows format issue.

 

The other thing I would check is whether or not your date is actually a string.  It looks like one but it might not be and you may need to convert it to one before you can use it in a file name.

View solution in original post

Highlighted
New Contributor

Re: renameTo + tiimestamp

A co worker just found this solution for me that works, I owe him :-)

 

import groovy.io.FileType

def now = new Date()
def oldfile = new File("C:/temp/ISPC-OUTPUT.xlsx")

String empty = ""
oldfile.parentFile.eachFile (FileType.FILES)
{
file ->
curDate = now.format("yyyyMMdd", TimeZone.getTimeZone('UTC'))
String newName = file.path
newName = newName.replaceAll(~/ISPC-OUTPUT/, "ISPC-OUTPUT"+"_"+curDate)
file.renameTo newName
log.info "New Name:- "+newName
}

View solution in original post

Highlighted
Community Manager

Re: renameTo + tiimestamp

Thank you Marsha for your reply. 

Hi @JesperJ , great that you’ve found a solution. Thanks for sharing with the Community! 



Sonya Mihaljova
Community and Education Specialist

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!