1. 程式人生 > >Groovy_遍歷檔案目錄

Groovy_遍歷檔案目錄

Common method:

1. void eachDir(Closure closure): Invokes the closure for each subdirectory in this directory, ignoring regular files.

class eachDirTest {
        public static void main(String[] args) {
            def count = 0
            def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests"
) dir.eachDir{directory-> println directory } } }

2. void eachDirMatch(Object nameFilter, Closure closure): Invokes the closure for each subdirectory whose name (dir.name) matches the given nameFilter in the given directory - calling the DefaultGroovyMethods#isCase(java.lang.Object, method to determine if a match occurs.

class eachDirTest {
    public static void main(String[] args) {
        def count = 0
        def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests")

        dir.eachDirMatch(~/^Sanity.*/){d ->
            println d
        }
    } 
}

Output:
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite1
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite2
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite3
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite4
D:\Project\SoapUI\Project-smoke-tests\SanityTestSuite5
……..

3. eachDirRecurse(File self, Closure closure) : Invokes the closure for each descendant directory of this directory.

class eachDirTest {
        public static void main(String[] args) {
            def count = 0
            def dir = new File("D:\\Project\\SoapUI\\Project-smoke-tests")

            dir.eachDirRecurse{directory->
                println directory
            }
        }  
}