Retrieve a list of files from a remote server?! - WeOnlyDo Discussion board

Retrieve a list of files from a remote server?! (General questions)

by Kamal, Thursday, January 13, 2005, 13:49 (7051 days ago)

Hi,

I would like to retrieve all the files in a specified directory in a remote server? how can i do that?

it would be nice to have it in C# code please?

(Note: the names of the files are not specified. i.e. the file can have any names, the only known attribute is the directory name!)

Thank you and hope to hear from you very soon.

Kamal

Re: Retrieve a list of files from a remote server?

by wodSupport, Thursday, January 13, 2005, 13:54 (7051 days ago) @ Kamal

Kamal,

do you need to list all files, or download them? If you need to list them, please use ListDir method. If you need to download them, please refer to this post: http://www.weonlydo.com/index.asp?forum=1&action=view&topic=1101059840

Re: Retrieve a list of files from a remote server?

by Kamal, Thursday, January 13, 2005, 15:21 (7051 days ago) @ wodSupport

I'm trying to download them and remove them from that remote directory!

I've written the following code:

----------------------------------------------------------
public static SortedList fileNames;

private static void sftp_AttributesDataEvent(object Sender, SFTP.AttributesArgs[] Args)
{
for (int i=0;i<Args.Length;i++)
{
//if(Args.Name != . && Args.Name != .. )
{
fileNames.Add(i, Args.Name);
}
}
}

private void ExecuteSFTPMethods()
{
sftp = new SFTP();

sftp.Blocking = true;
sftp.Hostname = this.txtHostname.Text;
sftp.Login = this.txtLogin.Text;
sftp.Password = this.txtPassword.Text;

sftp.Connect();

sftp.LocalPath = this.txtLocalPath.Text.Replace( DateTime , { + DateTime.Now.Day.ToString() + } );
sftp.RemotePath = this.txtRemotePath.Text;

sftp.AttributesDataEvent += new SFTP.AttributesDataDelegate(sftp_AttributesDataEvent);

fileNames = new SortedList();

try
{
sftp.ListAttributes(sftp.RemotePath);

for ( int i = 0; i < fileNames.Count; i++ )
{
sftp.GetFile(sftp.LocalPath, sftp.RemotePath + fileNames.GetByIndex(i));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
sftp.Disconnect();
-------------------------------------------------------------

The directory has 2 files, but the list above (fileNames sortedlist) return 4 files, the first file and the second file are the . and .. and the rest are the correct file names.

In addition, the remotePath gets changed after getting the file (i.e. executing the GetFile() method). why is that?

So in short, i need a safe way to retrieve all the files ONLY, no directories or any other thing..!?

Thank you.

Re: Retrieve a list of files from a remote server?

by wodSupport, Thursday, January 13, 2005, 15:26 (7051 days ago) @ Kamal

Kamal,

I think you answered all questions by yourself. As you can see, remote list contains . and .. Not much we can do about it - this is part of UNIX/DOS filesystems. I see in your code you check if filename= .. or filename= . in which case you ignore them. That's exactly what I would suggest.

As for RemotePath being changed internally - this is also correct. So, just provide it again from your code.

The way you're doing it now is safe way.

Re: Retrieve a list of files from a remote server?

by Kamal, Friday, January 14, 2005, 17:42 (7050 days ago) @ wodSupport

thanks alot of your help.

However, Now, I would like to be able to convert the files retrieved to a Stream, How can I do that??

the code i'm using looks something like this:
-----------------------------------
sftp.GetFile(remoteDir + fileNames.GetByIndex(i)); //RemoteFile
StreamReader sr = new StreamReader(); //the changes should be here but what and how?

NewMsgStream = new MemoryStream();
StreamCopy(NewMsgStream, sr.BaseStream);
sr.BaseStream.Close();
fileNames.Remove(i);
sftp.DeleteFile();
------------------------------------

Note: I'm trying to convert it into a stream as I'm not interested into putting it somewhere locally. instead passing it to some other server and/or web service to process the Stream!)

Any suggestions please?

Re: Retrieve a list of files from a remote server?

by wodSupport, Friday, January 14, 2005, 20:14 (7050 days ago) @ Kamal

Kamal,

I think you should really consult our helpfile, it has it all there.

If you plan to list filenames to a stream use

public void ListDir(string,Stream);

if you plan to download file to a stream, use public void GetFile(Stream,string);