NewUser
NewUser

Reputation: 27

C# WinSCP .NET assembly Download the latest file from the latest directory

I'm new to WinSCP. I'm facing difficulty of making the remote path to dynamic because the folder in my FTP is generated following by root/data/20160222/00(hour)/00(minute)/test.json*

This path also always contain more than one files.

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "192.168.1.100",
    UserName = "admin",  
    Password = "admin",      
};

string localPath = @"c:\\gatewayftp\\json";
// this path needs to take the latest date and the latest hour and minutes every day
string remotePath = "/data/20160228/2100/59" 

Now I set fixed path, struggle for solution.

Upvotes: 1

Views: 1658

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

Expanding on the WinSCP .NET assembly example for Downloading the most recent file:

string remotePath = "/data";
// In each of three levels of hierarchy...
for (int i = 0; i < 3; i++)
{
    // ... pick the last file/directory alphabetically
    // (use .LastWriteTime instead of .Name to pick the latest file/directory by time)
    remotePath +=
        "/" + 
        session.ListDirectory(remotePath).Files
            .OrderByDescending(file => file.Name).First().Name;
}

See also the documentation for Session.ListDirectory.

Upvotes: 2

Related Questions