HINT: download all files from remote folder - WeOnlyDo Discussion board

HINT: download all files from remote folder (General questions)

by wodSupport, Sunday, November 21, 2004, 18:57 (7106 days ago)

This is VB6 code (but can easily be applied to other languages) that will download all files from remote folder, in async mode. It will first get list of all files, and then download them one by one. You must change the code to set your Login, Password, Hostname, and also LocalDir and RemoteDir.

[code]Option Explicit
Dim WithEvents SFTP As wodSFTPCom
Dim RemoteDir As String
Dim LocalDir As String
Dim FileList As Collection

Private Sub Form_Load()
Set SFTP = New wodSFTPCom
SFTP.Login = **login**
SFTP.Password = **password**
SFTP.HostName = **hostname**

RemoteDir = /home2/kreso/udp/
LocalDir = c:\a\
Set FileList = New Collection

SFTP.Connect
End Sub

Private Sub SFTP_AttributesData(ByVal Items As wodSFTPCOMLib.ISftpItems)
Dim i As Integer
Dim item As SftpItem

For Each item In Items
If (item.Permissions And 16384) = 0 Then
FileList.Add item.Name
End If
Next
End Sub

Private Sub SFTP_Connected(ByVal ErrorCode As Integer, ByVal ErrorText As String)
If ErrorCode = 0 Then
SFTP.ListAttributes RemoteDir
Else
MsgBox ErrorText
End If
End Sub

Private Sub SFTP_Disconnected()
MsgBox Done!
End Sub

Private Sub SFTP_Done(ByVal ErrorCode As Integer, ByVal ErrorText As String)
Dim fname As String

If ErrorCode <> 0 Then
Debug.Print ERROR: & ErrorText
End If

If FileList.Count > 0 Then
fname = FileList.item(1)
FileList.Remove 1

Debug.Print Downloading & fname
SFTP.GetFile LocalDir, RemoteDir + fname
Else
SFTP.Disconnect
End If
End Sub
[/code]

Re: HINT: download all files from remote folder

by sumeetkoshal, Tuesday, January 04, 2005, 11:05 (7062 days ago) @ wodSupport

Hi,
U can write following piece of code in VC to do the same.
write a function called FindFiles which will return List of all the
files [',' SEPARATED] in a given directory except directory in it.
ex:
BOOL FindFiles(CString sDir, CString & sList)
{
CString sFiles;
BSTR bstrList;
USES_CONVERSION;
HRESULT hr=NULL;

hr = m_FtpCom->ListNames((_variant_t)sDir.AllocSysString());
if(!SUCCEEDED(hr))
return FALSE;

hr = m_FtpCom->get_ListItem(&bstrList);
if(!SUCCEEDED(hr))
return FALSE;

sFiles.Format(_T( s ),bstrList);

CString Sfile,Sdir;
int index1,index2;

while(sFiles.GetLength()!=0)
{
index1=sFiles.Find(_T( / ));
index2=sFiles.Find(_T(
));
if(index2<index1 || index1 == -1)
{//here u will get the filename
Sfile= sFiles.Left(index2-1);
sFiles=sFiles.Right(sFiles.GetLength()-index2-1);
sList = sList + Sfile + _T( , );
}
else
{//here u will get the directory name
Sdir= sFiles.Left(index2-1);
sFiles=sFiles.Right(sFiles.GetLength()-index2-1);
}

}
if(sList.GetLength()>0)
sList=sList.Left(sList.GetLength()-1);
//sList now contains the list of all the filenames[','
SEPARATED present in the given directory.
}

//USE THE ABOVE FUNCTION TO GET THE LIST OF FILENAMES
//set csFiles = sList

while(csFiles.GetLength()>0)
{
iIndex = csFiles.Find(_T( , ));
if(iIndex !=-1 )
{
sFname = csFiles.Left(iIndex);
csFiles = csFiles.Right(csFiles.GetLength()-iIndex-1);
}
else
{
sFname = csFiles;
csFiles=_T( );
}
//since u have the filename here use GetFile method
}//end of while loop

//at the end of this while loop u have all the files at the desired location


Regards
Sumeet