简体   繁体   English

Groovy / jenkins:重命名文件

[英]Groovy / jenkins : rename file

I would like to rename lastModified() json as filename+"processing" in jenkins with groovy. 我想用groovy在jenkins中将lastModified()json重命名为filename +“ processing”。 I am unsuccessfully doing : 我没有成功:

JSON_BASE_PATH="/json_repo/"

def file = new File(JSON_BASE_PATH).listFiles()?.sort { it.lastModified() }?.find{it=~/.json$/}
file.renameTo( new File( file.getName() + ".processing") )
print "Filename is : " + file

How to rename it ? 如何重命名呢?

You actually already have the answer in your code, you're just not storing it in a variable! 实际上,您的代码中已经有了答案,只是没有将其存储在变量中! new File( file.getName() + ".processing")

An instance of File isn't the actual entry on the file system, it's just a representation of one. File的实例不是文件系统上的实际条目,它只是一个实例的表示 So after you perform the rename, you need to work with the File instance that represents the renamed file system entry: 因此,在执行重命名之后,您需要使用代表重命名的文件系统条目的File实例:

JSON_BASE_PATH="/json_repo/"

def file = new File(JSON_BASE_PATH).listFiles()?.sort { it.lastModified() }?.find{it=~/.json$/}
def modifiedFile = new File("${file.getName()}.processing")

/* Check their existence */
println "${file.getName()} exists? ${file.exists()}"
println "${modifiedFile.getName()} exists? ${modifiedFile.exists()}"

/* Rename the file system entry using the File objects */
file.renameTo(modifiedFile)

/* See what we have */
println "Original filename is: ${file}"
println "${file.getName()} exists? ${file.exists()}"
println "Modified Filename is: ${modifiedFile}"
println "${modifiedFile.getName()} exists? ${modifiedFile.exists()}"

Update : renameTo is working fine. 更新:renameTo工作正常。 However file var is not reflecting the rename name. 但是,文件var不能反映重命名名称。 How to get new rename name ? 如何获得新的重命名名称?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM