JSI Tip 8297. Anonymous FTP download script.

Borrowing from http://www.paulsadowski.com/WSH/getremotebinaryfile.htm, I have scripted FTPVBS.BAT and FTPVBS.VBS to download a binary, or text file, using FTP.

The syntax for using FTPVBS.BAT is:

call FTPVBS URL Folder FileName \[OK\]

Where URL is the URL of the file you wish to download, Folder is the destination folder where you will store the downloaded file, and FileName is the file name to be stored in Folder.

NOTE: OK is an optional fourth parameter, a call directed environment variable, that will contain a Y if FileName was downloaded, and an N if it was not.

NOTE: FTPVBS.BAT and FTPVBS.VBS must be located in the same folder.

FTPVBS.BAT contains:

@echo off
if \{%3\}

\{\} goto err set URL=%1 set fldr=%2# set file=%3 set fldr=%fldr:"=% set fldr=%fldr:\#=% set fldr="%fldr:#=%\" set file="%file:"=%" set ff=%fldr%%file% set ff="%ff:"=%" if exist %ff% del /q %ff% set ftpv="%~DP0FTPVBS.vbs" cscript //NOLOGO %ftpv% %URL% %fldr% %file% if \{%4\}

\{\} endlocal&goto :EOF set found=N if exist %ff% set found=Y endlocal&set %4=%found% goto :EOF :err @echo Syntax: FTPVBS URL Folder FileName \[OK\] endlocal
FTPVBS.VBS contains:
Dim objArgument
Set objArgument = Wscript.Arguments
Set xml = CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", URL, False, "user", "pass"
set oStream = createobject("Adodb.Stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist = 1
oStream.type = adTypeBinary
oStream.write xml.responseBody
' Do not overwrite an existing file
oStream.savetofile DestFolder & ImageFile, adSaveCreateNotExist
' Use this form to overwrite a file if it already exists
' oStream.savetofile DestFolder & ImageFile, adSaveCreateOverWrite
set oStream = nothing
Set xml = Nothing

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.