' You may specify a wildcard in filename (e.g. *.txt).
' NB: You need to have C:\winnt\system32\wshom.ocx registered to use the WSCRIPT.SHELL object.
' It is registered by default, but is sometimes removed for security reasons.
' You will also need cmd.exe in the path, which again is there, unless the box is locked down.
Dim oTextFile, oScript, oScriptNet, FSO, oFile, strCMD, strTempFile
Dim ArrgObj, address, username, password, temp_dir, file_dir, filename, remote_dir
Dim myarray, LogStr, line, code, fname, status, specs, tostr
' ### Read in command line values. ###
Set ArgObj = WScript.Arguments
address = ArgObj(0)
username = ArgObj(1)
password = ArgObj(2)
temp_dir = ArgObj(3)
file_dir = ArgObj(4)
filename = ArgObj(5)
If ArgObj.length > 6 Then
remote_dir = ArgObj(6)
Else
remote_dir = ""
End If
set ArgObj = Nothing
' Use the below values for hardcoded parameters.
' Edit these variables to match your specifications
address = "[URL unfurl="true"]www.somewhere.com"[/URL]
username = "username"
password = "password"
temp_dir = "C:\" 'Local folder where temporary files are created.
file_dir = "C:\" 'Path of folder containing files to FTP.
remote_dir = "" ' Leave blank if uploading to root directory
filename = "testing.txt" ' You can use wildcards here (e.g. *.txt)
' On Error Resume Next
Set oScript = CreateObject("WSCRIPT.SHELL")
Set FSO = CreateObject("Scripting.FileSystemObject")
' ### Build the ftp commands file ###
Set oTextFile = FSO.CreateTextFile(temp_dir & "temp.ftp")
oTextFile.WriteLine "lcd " & file_dir
oTextFile.WriteLine "open " & address
oTextFile.WriteLine username
oTextFile.WriteLine password
' If destination folder is not the root then issue cd command to change folders.
If remote_dir <> "" Then
oTextFile.WriteLine "cd " & remote_dir
End If
oTextFile.WriteLine "prompt"
' Set to binary mode.
oTextFile.WriteLine "binary"
' If there are multiple files use the command 'mput', instead of 'put'
If Instr(1, filename, "*",1) Then
oTextFile.WriteLine "mput " & file_dir & filename
Else
oTextFile.WriteLine "put " & file_dir & filename
End If
oTextFile.WriteLine "bye"
oTextFile.Close
Set oTextFile = Nothing
' ### Execute the FTP.exe program and capture screen output to temporary file
' Use cmd.exe to run ftp.exe, parsing our newly created command file
strCMD = "ftp.exe -s:" & temp_dir & "temp.ftp"
strTempFile = temp_dir & FSO.GetTempName( )
' Pipe output from cmd.exe to a temporary file
Call oScript.Run ("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
Set oScript = Nothing
' ### Grab output from temporary file and parse it for output to log file. ###
Set oFile = FSO.OpenTextFile (strTempFile, 1, False, 0)
' On Error Resume Next
If remote_dir = "" Then
tostr = "Root"
Else
tostr = remote_dir
End If
LogStr = Now() & vbCrLf & "Copying " & filename & " from: " & file_dir & " to: " & tostr & " on " & address & vbCrLf
While not oFile.AtEndOfStream
line = oFile.ReadLine()
code = Left(line, 3)
Select Case code
Case "150" ' Output fname and stats for successful or fname and error message.
' Get the fname from the end of the string.
myarray = Split(line)
fname = myarray(Ubound(myarray))
status = oFile.ReadLine()
If Left(status, 3) = "226" Then
specs = oFile.ReadLine()
LogStr = LogStr & fname & " - " & Right(specs, Len(specs)-5) & vbCrLf
Else
LogStr = LogStr & fname & ": " & "ERROR " & status & vbCrLf
End If
Case "421"
LogStr = LogStr & "ERROR 421: Temporary Error - Service not available, closing control connection." & vbCrLf
Case "425"
LogStr = LogStr & "ERROR 425: Temporary Error - Cannot open data connection." & vbCrLf
Case "426"
LogStr = LogStr & "ERROR 426: Temporary Error - Connection closed; transfer aborted." & vbCrLf
Case "450"
LogStr = LogStr & "ERROR 450: Temporary Error - Requested file action not taken. File unavailable (for example, file busy)." & vbCrLf
Case "451"
LogStr = LogStr & "ERROR 451: Temporary Error - Requested action aborted: Local error in processing." & vbCrLf
Case "452"
LogStr = LogStr & "ERROR 452: Temporary Error - Requested action not taken. Insufficient storage space in system." & vbCrLf
Case "500"
LogStr = LogStr & "ERROR 500: Permanent Error - Syntax error, command unrecognized. This may include errors such as command line too long." & vbCrLf
Case "501"
LogStr = LogStr & "ERROR 501: Permanent Error - Syntax error in parameters or arguments." & vbCrLf
Case "502"
LogStr = LogStr & "ERROR 502: Permanent Error - Command not implemented." & vbCrLf
Case "503"
LogStr = LogStr & "ERROR 503: Permanent Error - Bad sequence of commands." & vbCrLf
Case "504"
LogStr = LogStr & "ERROR 504: Permanent Error - Command not implemented for that parameter." & vbCrLf
Case "530"
LogStr = LogStr & "ERROR 530: Permanent Error - Not logged in." & vbCrLf
Case "532"
LogStr = LogStr & "ERROR 532: Permanent Error - Need account for storing files." & vbCrLf
Case "550"
LogStr = LogStr & "ERROR 550: Permanent Error - Requested action not taken. File unavailable (for example, file not found, no access)." & vbCrLf
Case "551"
LogStr = LogStr & "ERROR 551: Permanent Error - Requested action aborted: Page type unknown." & vbCrLf
Case "552"
LogStr = LogStr & "ERROR 552: Permanent Error - Requested file action aborted. Exceeded storage allocation (for current directory or dataset)." & vbCrLf
Case "553"
LogStr = LogStr & "ERROR 553: Permanent Error - Requested action not taken. File name not allowed." & vbCrLf
End Select
Wend
LogStr = LogStr & vbCrLf
oFile.Close
' Delete the output response & ftp-command files
Call FSO.DeleteFile( strTempFile, True )
Call FSO.DeleteFile( temp_dir & "temp.ftp", True )
Set FSO = Nothing
' Print result of FTP session to screen
MsgBox LogStr