Jirong Hu Jirong Hu - 1 year ago 200
Groovy Question

How to find a .zip file in the current directory using Groovy?

I have a .zip file in the current directory, I want to get its file name using Groovy. e.g. if the file is myfile.zip, I want to get the "myfile" part. Can anyone give me a code snip? Thanks.

Answer Source

Something like this should work:

filename=new File("directory").listFiles().find{it.name.endsWith(".zip")}

If you don't want the .zip on the end, subtract it:

filename=new File("directory").listFiles().find{it.name.endsWith(".zip")}.name - ".zip"

(By the way, the first one ends up with a file object--you can do whatever you want with it. The second ends up with the string that is the name without the .zip)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download