Groovy-如何使用内部变量创建路径目录 [英] Groovy - How to create path directory with variable inside

查看:55
本文介绍了Groovy-如何使用内部变量创建路径目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在groovy中有一个脚本来创建带有结果的文本文件

I have a script in groovy to create a text file with results

我定义的路径文件是C:/temp/SoapTest/,我想动态创建一个新文件夹,该文件夹在任何新的用户测试中都会更改,因此 我想使用一个定义的变量作为 ,因此在这种情况下,变量为受影响的CI".

My defined path file is C:/temp/SoapTest/ and I want to create dynamically a new folder , which changes in any new user test so I want to use a defined variable as the name of the folder , so the variable in this case is Affected CI .

这是我的完整代码:

nl=System.getProperty("line.separator");
def Date0 = testRunner.testCase.testSuite.getPropertyValue( "Date")
def DateIn = testRunner.testCase.testSuite.getPropertyValue( "DateIn")
def Summary = testRunner.testCase.testSuite.getPropertyValue( "Summary")
def SummaryIn = testRunner.testCase.testSuite.getPropertyValue( "SummaryIn")
def RandomID = testRunner.testCase.testSuite.getPropertyValue( "RandomID")
def AffectedCI = testRunner.testCase.testSuite.getPropertyValue( "AffectedCI")
def SourceStatus = testRunner.testCase.testSuite.getPropertyValue( "SourceStatus")
def event = 'Active-Inactive'
def INC = testRunner.testCase.testSuite.getPropertyValue( "INC")
def IID_Active = testRunner.testCase.testSuite.getPropertyValue( "IID Active")
def IID_Inactive = testRunner.testCase.testSuite.getPropertyValue( "IID Inactive")
class LeftShiftNewlineCategory {
    static Writer leftShift(Writer self, Object value) {
        self.append value + "\n\r"
    } 
}
use(LeftShiftNewlineCategory) {
def pathfile = 'C:/temp/SoapTest/'
def subpath = 'Active-Inactive '
def fileformat = '.txt'
def FinRes = AffectedCI
def datetime = new Date().format(" dd MM yy' 'HH-mm-ss") 
def file = new File(pathfile + subpath + FinRes + datetime + fileformat).withWriter { out ->
        out << "Date:" +"\t"+"\t"+"\t"+ Date0  
        out << "${nl}DateIn:" +"\t"+"\t"+"\t"+ DateIn 
        out << "${nl}RandomID:" +"\t"+"\t"+ RandomID
        out << "${nl}AffectedCI:" +"\t"+"\t"+ AffectedCI 
        out << "${nl}SourceStatus" +"\t"+"\t"+ SourceStatus
        out << "${nl}event" +"\t"+"\t"+"\t"+ event
        out << "${nl}INC" +"\t"+"\t"+"\t"+ INC 
        out << "${nl}IID Active" +"\t"+"\t"+ IID_Active
        out << "${nl}IID Inactive" +"\t"+"\t"+ IID_Inactive 
        out << "${nl}"               
        out << "${nl}Payload:" +"\t"+"\t"+ Summary        
        out << "${nl}"               
        out << "${nl}PayloadIn:" +"\t"+"\t"+ SummaryIn
    }
}

非常感谢您!

推荐答案

是否可能需要首先创建父目录:

Could be you need to create the parent directory(ies) first:

def file = new File(pathfile + subpath + FinRes + datetime + fileformat)
def success = file.parentFile.mkdirs()
println "created ${file.parentFile}: ${success}"
file.withWriter { out ->
  ...
}

这篇关于Groovy-如何使用内部变量创建路径目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆