'How to read file names from a given path on server using Groovy

I have multiple files on the given FTP path. I want to read the latest file received, for which I need to read all the file names in the given path. I am using Groovy on the OUAF based product, ORMB. Please suggest on how to read the file names. Also, new File() statement is not supported.



Solution 1:[1]

Assuming you want to use apache commons-net module you can do this:

@Grab(group='commons-net', module='commons-net', version='3.7')
import org.apache.commons.net.ftp.FTPClient

String hostname = "some-server.some-domain.com"
String username = "someUser"
String password = "somePassword"
String path = "/var/some/path"

println("About to connect....");
new FTPClient().with {
    connect hostname
    enterLocalPassiveMode()
    login username, password
    changeWorkingDirectory path
    FTPFile latest = listFiles().max { it.timestamp.time }
    println("Downloading ${latest.name}..")
    File incomingFile = new File( latest.name )
    incomingFile.withOutputStream { os -> retrieveFile(latest, os) }
    disconnect()
}
println("...Done.");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chubbsondubs