Mandy
Mandy

Reputation: 3

Vbscript logging into ftp

I am attempting to log into ftp host and download files. I am not sure how to locate the file as it is stored in directories under the date and will change every day. this is what I have so far

Option Explicit
Dim objFSO, objMyFile, objShell, strFTPScriptFileName, strFilePut
Dim strLocalFolderName, strFTPServerName, strLoginID
Dim strPassword, strFTPServerFolder


strLocalFolderName = "c:\foldername"
strFTPServerName = "ftp.host.com"
strLoginID = "somelogin"
strPassword = "password8"

so after the password how would I log into the date file and locate so for example the file would be under

20130722/filename.ftp

Upvotes: 0

Views: 3009

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

The usual way to do this in VBScript is to generate an FTP script and run that with ftp.exe:

'variable definitions
...

Function qq(str)
  qq = Chr(34) & str & Chr(34)
End Function

Set fso = CreateObject("Scripting.FileSystemObject")
Set sh  = CreateObject("WScript.Shell")

remoteDir = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2)
tempDir   = sh.ExpandEnvironmentStrings("%TEMP%")
script    = fso.BuildPath(tempDir, "download.ftp")
logfile   = fso.BuildPath(tempDir, "ftp.log")

Set f = fso.OpenTextFile(script, 2, True)
f.WriteLine "open" & strFTPServerName & vbNewLine _
  & "user" & strLoginID & vbNewLine _
  & strPassword & vbNewLine _
  & "prompt no" & vbNewLine _
  & "lcd " & strLocalFolderName & vbNewLine _
  & "cd " & remoteDir & vbNewLine _
  & "get cs.ftp" & vbNewLine _
  & "bye"
f.Close

rc=sh.Run("%COMSPEC% /c ftp -s:" & qq(script) & " >" & qq(logfile), 0, True)

WScript.Echo "FTP finished with exit code " & rc & "."

fso.DeleteFile script, True

The above should work out of the box. If you're free to install additional software, the FTP client included with ActiveXperts' Network Component might be another option.

Upvotes: 1

Related Questions