gpt4 book ai didi

file - 常规/ Jenkins : rename file

转载 作者:行者123 更新时间:2023-12-02 15:00:49 26 4
gpt4 key购买 nike

我想在 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

如何重命名?

最佳答案

实际上您的代码中已经有了答案,只是没有将它存储在变量中! 新文件(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()}"

关于file - 常规/ Jenkins : rename file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com