DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Deleting All .class Files with Groovy

Deleting All .class Files with Groovy

Dustin Marx user avatar by
Dustin Marx
·
Apr. 23, 12 · Interview
Like (0)
Save
Tweet
Share
5.30K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Groovy (programming language)

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Chat GPT-3 Changed the Life of Young DevOps Engineers
  • Keep Your Application Secrets Secret
  • Best Navicat Alternative for Windows
  • 19 Most Common OpenSSL Commands for 2023

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: