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

Advertisement

albogado
Joined:
Posts:
2
Location:
Chicago

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

Hello,
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 = "hostname.host.com",
UserName = "user",
Password = "yEalB",
SshHostKeyFingerprint = "ssh-rsa 2048 a9:86:dc:0b:8f:e4:70:f1:dc:0d:26:3b:94:4f:52:41"
};

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,809
Location:
Prague, Czechia

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

Use Session.ListDirectory.
https://winscp.net/eng/docs/library_session_listdirectory

Iterate returned list of files and look for old files using RemoteFileInfo.LastWriteTime.
https://winscp.net/eng/docs/library_remotefileinfo

Call Session.RemoveFiles for every matching file.
https://winscp.net/eng/docs/library_session_removefiles

Reply with quote

albogado
Joined:
Posts:
2
Location:
Chicago

Thank you

Thank you, this information is helpful. Hopefully I can get this working and share my code with the community. Thanks again!

Reply with quote

Advertisement

You can post new topics in this forum