Groovy_ traversing the file directory

Source: Internet
Author: User
Tags closure soapui

Official Documentation: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html

Common Method:

1. Void Eachdir (Closure Closure): invokes the Closure for each subdirectory in this directory, ignoring regular F Iles.

Class Eachdirtest {public
        static void Main (string[] args) {
            def count = 0
            def dir = new File ("D:\\project\\so Apui\\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\\so Apui\\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}}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.