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-m...
  • Marsha_R's avatar
    5 years ago

    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
    }