Reputation: 87
I am new for SFTP Server. I am tried to upload and download files using SFTP server. It's a stand alone application. While downloading files from remoter server to local server, I able to download list of files under specified path using following SFTP Code:
sftp.lcd(details.get("LOCAL_DIR"));
sftp.cd(details.get("REMOTE_DIR"));
List<SftpFile> remoteFiles = sftp.ls();
for(int i = 0 ; i < remoteFiles.size(); ++i) {
if(remoteFiles.get(i).isFile()) {
String remoteFile = remoteFiles.get(i).getFilename();
sftp.get(remoteFile);
System.out.println("[SFTPOperations][downLoad] Downloaded: " + remoteFile);
System.out.println("Remote File: " + remoteFile);
System.out.println("Remote Archive Dir: " + details.get("REMOTE_ARCHIVE_DIR"));
sftp.rename(remoteFiles.get(i).getFilename(), details.get("REMOTE_ARCHIVE_DIR"));
System.out.println("[SFTPOperations][downLoad] Archived: " + remoteFile);
}
}
By using above code, its downloading all the files from remote directory to local directory. Here I want to download files from remote directory which are matching given pattern.
E.g. pattern: Query*.txt
I want to download text files which are starts with Query
.
Please suggest how to do the mentioned above.
Upvotes: 2
Views: 3065
Reputation: 1
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace FileDownload
{
class Program
{
static void Main(string[] args)
{
string date = DateTime.Now.ToShortDateString();
using (StreamWriter writer = File.CreateText(ConfigurationManager.AppSettings["localDirectory"]+"U2000_CSVFile_Fetching_log_"+date+".txt"))
{
try
{
string host = ConfigurationManager.AppSettings["host"];
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
string remoteDirectory = ConfigurationManager.AppSettings["remoteDirectory"]+date+"/";
Console.WriteLine(password);
string localDirectory = ConfigurationManager.AppSettings["localDirectory"] + date;
System.IO.Directory.CreateDirectory(localDirectory);
Console.WriteLine(localDirectory);
using (var sftp = new SftpClient(host, username, password))
{
sftp.Connect();
writer.WriteLine("Connection OK");
var files = sftp.ListDirectory(remoteDirectory);
foreach (var file in files)
{
string remoteFileName = file.Name;
writer.WriteLine(file.Name);
if ((!file.Name.StartsWith(".")))
{
using (Stream file1 = File.OpenWrite(localDirectory + remoteFileName))
{
writer.WriteLine(file.Name + " Download Started");
sftp.DownloadFile(remoteDirectory + remoteFileName, file1);
writer.WriteLine(file.Name + " Downloaded Successfully_"+date);
}
}
}
}
}
catch (Exception ex)
{
writer.WriteLine(ex.Message);
}
}
Console.WriteLine("U2000 .csv files fetching Scheduler is Running");
Console.ReadLine();
}
}
}
Upvotes: 0
Reputation: 1146
Did you try this one?
if (remoteFile.endsWith(".txt")) {
sftp.get(remoteFile);
}
However you could use some sort of regular expresion to validate if the file name is valid according to the pattern you are traing to evaluate
Upvotes: 1