HINT: How to delete all files from remote dir - WeOnlyDo Discussion board

HINT: How to delete all files from remote dir (General questions)

by wodSupport, Tuesday, June 15, 2004, 22:30 (7226 days ago)

Info applies to wodSFTP.NET and C#.

This code will delete all files from remote folder. It uses Blocking mode, but can be easily changed in async behavior. You can apply this to almost any other operation that deals with many files - first get list of files, then loop through the list for each file. Should be easy to change to VB.NET and other languages.

[code]
public static System.Collections.SortedList names;
static void Main(string[] args)
{
SFTP sftp = new SFTP();
sftp.AttributesDataEvent += new WeOnlyDo.Client.SFTP.AttributesDataDelegate(sftp_AttributesDataEvent);

sftp.Hostname = ... ;
sftp.Login = ... ;
sftp.Password = ... ;
sftp.Blocking = true;
sftp.Connect();

names = new System.Collections.SortedList();
try
{
sftp.ListAttributes( /tmp );
}
catch (Exception)
{}

while (names.Count>0)
{
sftp.DeleteFile( /tmp/ + names[0]);
names.RemoveAt(0);
}

// optionally we can remove this dir also
// sftp.RemoveDir( /tmp );

sftp.Disconnect();
}

private static void sftp_AttributesDataEvent(object Sender, WeOnlyDo.Client.SFTP.AttributesArgs[] Args)
{
for (int i=0;i<Args.Length;i++)
{
names.Add(i, Args.Name);
}
}
[/code]


Complete thread: