Dustin Marx is a software developer who enjoys identifying and using the correct tool for the job. In addition to writing software and writing a blog on software development, Dustin occasionally presents at conferences and writes articles. Dustin is a DZone MVB and is not an employee of DZone and has posted 230 posts at DZone. You can read more from them at their website. View Full User Profile

Deleting All .class Files with Groovy

04.22.2012
| 3240 views |
  • submit to reddit

When running simple Java tests either to learn how something works in Java or to build examples for my blog posts, I often store the examples in the same directory on my machine. I decided that I wanted to remove all the generated .class files from this directory. There are several approaches to doing this on a Linux machine, but I wanted to remove these files on a Windows machine. I know there are tools and ways to do this on Windows, but I knew it would take me less time to write a simple Groovy script to do it than to search online for such a tool. This post provides and briefly describes the simple Groovy script for removing .class files.

deleteClassFiles.groovy

#!/usr/bin/env groovy
if (args.length < 1)
{
   println "Please specify directory under which .class files should be removed"
   println "  (including .class files in sub-directories)."
   System.exit(-1)
}

def directoryName = args[0]
println "Remove .class files in ${directoryName} and its subdirectories..."
def directory = new File(directoryName)
def classPattern = ~/.*\.class/
directory.eachFileRecurse(groovy.io.FileType.FILES)
{ file ->
   if (file ==~ classPattern)
   {
      println "Deleting ${file}..."
      file.delete()
   }
}

The above script will remove all files that have names ending with a .class suffix in the provided directory or any of its sub-directories. The directory is specified via a single command-line argument to the script, so I did not use Groovy's built-in CLI support in this case and instead relied on Groovy's implicit availability of the args parameter representing the command line arguments.

The Groovy script featured in this post takes advantage of the Groovy GDK File extension's eachFileRecurse method to recursively iterate through the names of the files in the provided directory and its subdirectories. Another convenient Groovy feature employed in this script is application of Groovy regular expression support to specify the pattern (file names ending with ".class" extension) and then to use the pattern matcher in the conditional that decides which files are to be deleted.

The above script prints out the files to be deleted as it deletes them. One could easily enhance the script to accept a parameter that would display files that would be deleted without actually deleting them. For me, this script was more proof that Groovy is so easy to use that sometimes it's easier to write a new script in Groovy to accomplish daily tasks than it is to find existing tools and scripts online to download.

 

Published at DZone with permission of Dustin Marx, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Kevin Slater replied on Mon, 2012/04/23 - 1:28pm

if you have a linux system (or cygwin installed on Windows) you could use:

 

find . -name "*.class" -exec rm {} \;

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.