HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Downloading files based on most recent date and timestamp

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
recentdownloadingdatefilesbasedtimestampandmost

Problem

I have a requirement to download zipped files from an SFTP site using C# and Script Task within SSIS.

We have multiple files on the server with the same name, but with different date and timestamps.

Is there a way download the files based on most recent date and timestamp?

Solution

As the answer by @MarkSinkinson shows, you can use WinSCP .NET assembly.

There is an official example on WinSCP site for downloading the most recent file.

As it shows, to select and download the most recent file, use:

// Get list of files in the directory
RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);

// Select the most recent file
RemoteFileInfo latest =
directoryInfo.Files
.Where(file => !file.IsDirectory)
.OrderByDescending(file => file.LastWriteTime)
.FirstOrDefault();

// Any file at all?
if (latest == null)
{
throw new Exception("No file found");
}

// Download the selected file
session.GetFiles(RemotePath.EscapeFileMask(remotePath + latest.Name), localPath).Check();


For a full code, see the link.

See also the article on using the WinSCP .NET assembly from SSIS.

(I'm the author of WinSCP)

Context

StackExchange Database Administrators Q#77512, answer score: 5

Revisions (0)

No revisions yet.