Using JSch ChannelSftp: How to read multiple files with dynamic names?

Viewed 62408

I have to read a bunch of .CSV files with dynamic file names from a SFTP server. These files get generated every 15 minutes.

I am using JSch's ChannelSftp, but there is no method which would give the exact filenames. I only see an .ls() method. This gives a Vector e.g.

[drwxr-xr-x    2 2019     2019          144 Aug  9 22:29 .,
 drwx------    6 2019     2019          176 Aug 27  2009 ..,
 -rw-r--r--    1 2019     2019          121 Aug  9 21:03 data_task1_2011_TEST.csv,
 -rw-r--r--    1 2019     2019          121 Aug  9 20:57 data_task1_20110809210007.csv]

Is there a simple way to read all the files in a directory and copy them to another location?

This code works for copying a single file:

JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(SFTPWORKINGDIR);
channelSftp.get("data_task1_20110809210007.csv","data_task1_20110809210007.csv");
2 Answers
Related