Forum Discussion

AAB's avatar
AAB
Regular Contributor
4 years ago

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?   ...
  • AAB's avatar
    AAB
    4 years ago

    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