Skip to content

Instantly share code, notes, and snippets.

@pditommaso
Created July 5, 2012 12:28
Show Gist options
  • Save pditommaso/3053430 to your computer and use it in GitHub Desktop.
Save pditommaso/3053430 to your computer and use it in GitHub Desktop.
clean-folder
#!/bin/env groovy
/*
* Delete all the files which name ends with the suffix '_debug' except the most recent 10
*/
def cli = new CliBuilder(usage:'clean-folder.groovy [-d] <folder to clean>')
cli.d('delete old files')
def options = cli.parse(args)
if( options.arguments().size() != 1 ) {
cli.usage()
System.exit(1)
}
/*
* define the path on which look for the file to delete
*/
def path = new File( options.arguments()?.size()>0 ? options.arguments().get(0) : '.' )
println 'Running path cleaner for path: ' + path.absolutePath
/*
* Look for all files ending with '_debug' prefix
*/
def list = []
path.eachFileRecurse { File file ->
if( file.name ==~/.+_debug\..+/ ) { list.add(file) }
}
list = list.sort { a,b ->
// note: 'b - a' to get a descent ordered list: the latested modified will be the first entry
b.lastModified() <=> a.lastModified()
}
def remain = list.take(10)
list = list.drop(10)
if( list ) {
println "\nFile(s) to be deleted:"
list.each { println "- $it" }
}
if( remain ) {
println "\nFile(s) NOT deleted:"
remain.each { println "- $it" }
}
if( options.d ) {
list.each { File file -> if(file.exists()) file.delete() }
println "\nDone"
}
else {
println "\nRunning in dry-run mode. Use the '-d' to delete files"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment