Jirong Hu Jirong Hu - 1 month ago 13
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.


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)