Transferred from: http://atobs.blogspot.fr/2012/08/match-only-files-that-match-pattern-in.html#!/2012/08/ Match-only-files-that-match-pattern-in.html
Groovy:
In Groovy we can use the Eachdirrecurse and Eachfilematch () methods to get all the file names displayed.
def pattern = ~/.*\.java/
def dirname= "/usr/local/mysource"
new File ("$dirname"). Eachdirrecurse {dir-
Dir.eachfilematch (pattern) {myfile
println "$myfile"
}//Eachfilematch
}//Eachfilematch
Python:
In Python, we can list each matching file using "Glob"
Import OS, glob, sys
For root, dirs, files in Os.walk (' E:\\users '):
Os.chdir (Root)
# Find all files that match log*
logs = Glob.glob (' *log* ')
If logs:
For fname in logs:
FullPath = Os.path.join (root, fname)
# Identify Files of Len 3 lines long and delete them
Count = Len (open (FullPath). ReadLines ())
if Count = = 3 or Count = = 2:
print ' removing ', fullpath
Os.remove (fullpath)
Match files, match pattern in Groovy and Python