Groovy-打印路径中的目录

Groovy-打印路径中的目录,groovy,Groovy,给定文件的以下路径: /home/fixed/foler/myScript.grooy 如何获取myScript.grooy路径中的各个目录 最终,我希望将/home、/home/fixed、/home/fixed/文件夹以新行打印在日志文件中 日志文件的输出: /home /home/fixed /home/fixed/folder 这可以通过递归实现: String fileName = '/home/fixed/folder/myScript.groovy' def printFil

给定文件的以下路径:

/home/fixed/foler/myScript.grooy
如何获取myScript.grooy路径中的各个目录

最终,我希望将/home、/home/fixed、/home/fixed/文件夹以新行打印在日志文件中

日志文件的输出:

/home
/home/fixed
/home/fixed/folder

这可以通过递归实现:

String fileName = '/home/fixed/folder/myScript.groovy'

def printFilePath(String fileName) {
    File file = new File( fileName )

    if( file.path != '/' ) { printFilePath file.parentFile.absolutePath }
    else { return }

    if( !file.isFile() ) println file.absolutePath
}

printFilePath fileName