Topic "Need to delete the files on the server after we pull them."

Author Message
Philky001
[View user's profile]
Donor
Joined: 2015-11-24
Posts: 20
HI we are getting files from ecom site that has our customers orders. This is now working well. But in SCP manual process we then delete the files in that server, having permissions of course. The question is how to do this in the .net program.
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = ConfigurationManager.AppSettings["HostName"].ToString(),
UserName = ConfigurationManager.AppSettings["UserName"].ToString(),
Password = ConfigurationManager.AppSettings["Password"].ToString(),
SshHostKeyFingerprint = ConfigurationManager.AppSettings["SshHostKeyFingerprint"].ToString()
};

using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);

RemoteDirectoryInfo directory = session.ListDirectory(ConfigurationManager.AppSettings["RemotePath"].ToString());
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);


string fileName = fileInfo.Name;
string remotePath = ConfigurationManager.AppSettings["RemotePath"].ToString() + fileName;
// string localPath = "d:\\backup\\" + fileName;
string localPath = ConfigurationManager.AppSettings["LocalPath"].ToString() + fileName;


if (session.FileExists(remotePath))
{
bool download;
if (!File.Exists(localPath))
{
Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath);
download = true;
}
else
{
DateTime remoteWriteTime = session.GetFileInfo(remotePath).LastWriteTime;
DateTime localWriteTime = File.GetLastWriteTime(localPath);


download = true;
}

if (download)
{
// Download the file and throw on any error
session.GetFiles(remotePath, localPath).Check();

Console.WriteLine("Download to backup done.");
}
}
else
{
Console.WriteLine("File {0} does not exist yet", remotePath);
}
}
}

return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
}
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
The easiest way is to use remove parameter of the Session.GetFiles:

session.GetFiles(remotePath, localPath, true).Check();

https://winscp.net/eng/docs/library_session_getfiles
Advertisements

You can post new topics in this forum






Search Site

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!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License