Creating an FTP drop-box

Status
Not open for further replies.

Osiris

Golden Master
Messages
36,817
Location
Kentucky
Someone asked me if it was possible to create an FTP drop-box on the QuickLaunch bar to automate uploading of files to an FTP server. The idea would be that you simply dragged the file or files you want to upload onto the icon in the QuickLaunch bar, and it would upload them automatically.
Twenty minutes of notepadding later, and the answer was yes. What's more - it's surprisingly useful if you regularly upload files to a specific server.

The script below will connect to a specific server, log in automatically, and upload the dragged files automatically determining whether they need to by uploaded as binary or ASCII. It's pure VBScript and uses Windows' built-in FTP client, so no additional software is needed.

To use the script:

Copy and paste the script into Notepad or another pure text editor.
Set the username, password, server, and rootdir variables (highlighted below in red).
Save the script to a convenient location. Make sure you give it a .vbs extension, and you name it something other than ftp.vbs.
Create a shortcut to the script file, and drag the shortcut onto the QuickLaunch bar.
Script source
Option Explicit
'declare my variables
Dim WshShell
Dim oExec
Dim fso, ftpscrfile
Dim arg, scriptsrc, ext, tempAry
Dim username, password, server, rootdir, isBin, ftpscrfilename

'Set the constants I want to use as commands
const BINARY_FILE = "bin"
Const TEXT_FILE = "asc"

'Set the server details
username = "anonymous"
password = "blah@"
server = "aklsrvctxp05"
rootdir = "/"

'Write out the first bit of the ftpscript
'Turn on hashing to show progress
scriptsrc = "open " & server & vbCrLf & _
username & vbCrLf & _
password & vbcrLf & _
"cd " & rootdir & vbCrLf & _
"hash" & vbCrLf

'declare the objects I 'm going to use
Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

'get the command-line arguments - i.e the path and filename
'of what I want to upload
for each arg in wscript.arguments

'get the extension of the file to upload
tempAry = split(arg, ".")
ext = tempAry(ubound(tempAry))

'Check for the last character of the extension being "
'and trim it off if it is
if right(ext, 1) = chr(34) then ext = left(arg, len(arg) -1)

'Assume file is binary just in case
isBin = true

select case ext
case "html", "htm", "txt", "asp", "php", "aspx", "css", "inc", "js", "vbs", "reg"
isBin = false
case else
isBin = true
end select

'Write out the ftpscript for this file
select case isBin
case true
scriptsrc = scriptsrc & BINARY_FILE & vbCrLf
case false
scriptsrc = scriptsrc & TEXT_FILE & vbCrLf
end select

'Write out ftpscript to upload file
scriptsrc = scriptsrc & "put """ & arg & """" & vbCrLf
next

'close off the ftpscript
scriptsrc = scriptsrc & "bye" & vbCrLf

'Determine the temporary path and set the filename
ftpscrfilename = WshShell.ExpandEnvironmentStrings("%Temp%") & "\ftpscript.txt"

'Write out the ftpscript
set ftpscrfile = fso.CreateTextFile(ftpscrfilename, True)
ftpscrfile.write scriptsrc
ftpscrfile.close

'call the command-line ftp client and give it the name of the ftpscript
Set oExec = WshShell.Exec("cmd /c start /WAIT /MIN ftp -s:""" & ftpscrfilename & """")

'Wait until the upload's finished.
Do While oExec.Status = 0
WScript.Sleep 100
Loop

'Wait a bit longer for safety's sake
WScript.Sleep 100

'Delete the ftpscript
fso.DeleteFile ftpscrfilename, true

'Be a nice little coder and clear down my objects
set oExec = nothing
set ftpscrfile = nothing
set fso = nothing
set WshShell = nothing
 
Status
Not open for further replies.
Back
Top Bottom