Get a list of all the files in a directory (recursive)

前端 未结 4 948
灰色年华
灰色年华 2020-12-23 02:59

I\'m trying to get (not print, that\'s easy) the list of files in a directory and its sub directories.

I\'ve tried:

def folder = \"C:\\\\DevEnv\\\\Pr         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 03:26

    This is what I came up with for a gradle build script:

    task doLast {
        ext.FindFile = { list, curPath ->
            def files = file(curPath).listFiles().sort()
    
            files.each {  File file ->
    
                if (file.isFile()) {
                    list << file
                }
                else {
                    list << file  // If you want the directories in the list
    
                    list = FindFile( list, file.path) 
                }
            }
            return list
        }
    
        def list = []
        def theFile = FindFile(list, "${project.projectDir}")
    
        list.each {
            println it.path
        }
    }
    

提交回复
热议问题