AAB
4 years agoRegular Contributor
ReadyAPI- append date in a filename
Hi all,
I would like to fetch a file from my harddrive but each time I do so the DateTime should be appended after the filename and before the extension of the file.
how could I do that please?
What I have so far (but it doesn't work)
// get document from path and append with DateTime
def path = testRunner.testCase.testSuite.project.getPropertyValue("pathDir")
def getFile = new File(path + "\\CheatSheetGitBash.html")
log.info getFile
def date = new Date().format('yyMMddhhmm')
log.info date
//add a timestamp to the file
def readName = getFile.getName()
log.info readName
def fileDate = readName + "${date}" as String
log.info fileDate
Output:
Thu Aug 12 18:18:32 CEST 2021: INFO: CheatSheetGitBash.html2108120625
Of course this is not what I'm expecting. Expected output should be:
CheatSheetGitBash210812T1823.html (or something like that)
Thanks in advance.
AAB
- Please see if the below link is useful
https://stackoverflow.com/questions/1569547/does-groovy-have-an-easy-way-to-get-a-filename-without-the-extension Hi nmrao
Thanks for your response. I have given it a try and it worked! Thanks!
Solution:
// get document from path and append with DateTime //use the commons.io from apache //cfr https://community.smartbear.com/t5/API-Functional-Security-Testing/ReadyAPI-append-date-in-a-filename/m-p/221103#M49504 import org.apache.commons.io.FilenameUtils def path = testRunner.testCase.testSuite.project.getPropertyValue("pathDir") def getFile = new File(path + "\\CheatSheetGitBash.html") log.info getFile def date = new Date().format('yyMMddhhmm') log.info date //add a timestamp to the file def readName = getFile.getName() log.info readName //get the extension of the filename def getExtension = FilenameUtils.getExtension(readName) log.info getExtension //remove extension readName def filenameWithoutExtension = FilenameUtils.removeExtension(readName) log.info filenameWithoutExtension def addDate = filenameWithoutExtension + "${date}" as String log.info addDate def newFileName = addDate + "." + getExtension log.info newFileName