Groovy / jenkins:重命名文件

Col*_*s06 2 groovy file jenkins

我想在 jenkins 中使用 groovy 将 lastModified() json 重命名为文件名+“处理”。我没有成功地做:

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
Run Code Online (Sandbox Code Playgroud)

如何重命名?

bto*_*bto 6

您实际上已经在代码中找到了答案,只是没有将其存储在变量中! new File( file.getName() + ".processing")

的实例File不是文件系统上的实际条目,它只是一个的表示。因此,在执行重命名后,您需要使用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()}"
Run Code Online (Sandbox Code Playgroud)