Forum Discussion

JesperJ's avatar
JesperJ
New Contributor
5 years ago
Solved

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");
}
}
}

 

  • 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.

  • JesperJ's avatar
    JesperJ
    5 years ago

    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
    }

3 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    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.

    • JesperJ's avatar
      JesperJ
      New Contributor

      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
      }
      • sonya_m's avatar
        sonya_m
        SmartBear Alumni (Retired)

        Thank you Marsha for your reply. 

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