Topic "Newbie Help - Delete files older than "n" days using C#"

Author Message
[View user's profile]

Joined: 2014-12-16
Posts: 2
Location: Chicago
I am looking for some assistance in regards to getting some automated file deletions to occur via a program. To be more specific I am looking to do the following;

1) login to device
2) search for specific filenames older than "n" days;
3) delete files

As I am new to C# I am having some difficulty in understanding how to do this. I am able to successfully login to the appliance in question but I'm lost on how to proceed from here. Is there anyone here that could provide some help or provide some examples on how to do items (2) and (3) as aforementioned above? Thanks!

This code is working for login;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinSCP;

namespace WinSCPTest
class Program
public static int Main()

// Setup session options
SessionOptions sessionOptions = new SessionOptions
Protocol = Protocol.Sftp,
HostName = "",
UserName = "user",
Password = "yEalB",
SshHostKeyFingerprint = "ssh-rsa 2048 a9:86:dc:0b:8f:e4:70:f1:dc:0d:26:3b:94:4f:52:41"
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 27086
Location: Prague, Czechia
Use Session.ListDirectory.

Iterate returned list of files and look for old files using RemoteFileInfo.LastWriteTime.

Call Session.RemoveFiles for every matching file.
[View user's profile]

Joined: 2014-12-16
Posts: 2
Location: Chicago
Thank you, this information is helpful. Hopefully I can get this working and share my code with the community. Thanks again!

You can post new topics in this forum


What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License