Script to extract files info from ftp server

December 2016




Issue



Wonder if you could help me with the following please.

I would like to construct a script (in any language whichever it's easiest) which when run, it will log on to an existing ftp server using a generic account and password, then list the files in a specific folder that begins with the string "abc". The file names along with their creation date are then to be extracted on a csv file (or similar). Can any one be able to help me on this please?

Solution


Here is the script:
ftp.Hostname = "ftp.test.com"
ftp.Username = "User"
ftp.Password = "Pass"

' The default data transfer mode is "Active" as opposed to "Passive".
' Change it to Passive by setting the Passive property:
ftp.Passive = 1

' Establish an AUTH SSL secure channel after connection
' on the standard FTP port 21.
ftp.AuthTls = 1

' The Ssl property is for establishing an implicit SSL connection
' on port 990. Do not set it.
ftp.Ssl = 0
' Connect and login to the FTP server.
connectStatus = ftp.Connect()
If (connectStatus <> 1) Then
'MsgBox ftp.LastErrorText
'WScript.Quit
Main = DTSTaskExecResult_Failure
Else
dirStatus = ftp.ChangeRemoteDir("RMed")
If (dirStatus <> 1) Then
'MsgBox ftp.LastErrorText
'WScript.Quit
Main = DTSTaskExecResult_Failure
End If



'MsgBox ftp.getCurrentRemoteDir()

'specify the file path and name on the local machine
localInvoiceFile = "C:Documents and SettingsNathanMy Documents2dRMedInvoice_RMed.txt"
localPatientFile = "C:Documents and SettingsNathanMy Documents2dRMedPatient_RMed.txt"

'specify the file path and name on the remote machine
remoteInvoiceFile = "C:ClientsStratFile UploadsCustomer UploadsRMedInvoice_RMed.txt"
remotePatientFile = "C:ClientsStratFile UploadsCustomer UploadsRMedPatient_RMed.txt"
'MsgBox remoteInvoiceFile
'MsgBox remotePatientFile

'download the files from the remote server
transferStatus = ftp.getFile(remoteInvoiceFile, localInvoiceFile)
if (transferStatus <> 1) then
'MsgBox ftp.LastErrorText
Main = DTSTaskExecResult_Failure
else
Main = DTSTaskExecResult_Success
end if

transferStatus = ftp.GetFile(remotePatientFile, localPatientFile)
if (transferStatus <> 1) then
'MsgBox ftp.LastErrorText
Main = DTSTaskExecResult_Failure
else
Main = DTSTaskExecResult_Success
end if

END IF
ftp.Disconnect

End Function

Note


Thanks to cb4nd17 for this tip on the forum.

Related :

This document entitled « Script to extract files info from ftp server » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.