• ARTICLES
SEARCH

How-To Geek

Upload Files to an FTP Site via a Batch Script

Outside of email, probably the most common way to send files to a remote party is via FTP. While there are a plethora of FTP clients you can choose from, Windows has an little known and under utilized command line FTP utility built in. The beauty of this tool lies in it’s ability to be scripted which we have harnessed in the batch script below.

This script can be used from the command line as a ‘no questions asked’ method of uploading one or many files with a single command. Additionally, you can call this script from batch files to perform automated file uploads. A few uses for this include (but, of course, not limited to):

  • Include in backup scripts to send data offsite.
  • Upload html/php/etc. files to a web server with a single command.
  • Create shortcuts to send a common group of files (such as a web site’s source pages).

Configuration

The only configuration required is to set the FTP server connection information. Under the “Connection information” line, set the following:

  • Server – The FTP Server you are uploading to. You can either enter the DNS name (ftp.myserver.com) or IP address (1.2.3.4).
  • UserName – Your user name for connecting to FTP server.
  • Password – Your password for connecting to the FTP server.

Depending on your firewall settings, the first time you run this script you may be prompted to allow FTP to connect to the Internet. Setting this to never prompt you again should remove future warnings.

The Script

@ECHO OFF
ECHO Upload to FTP
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.

REM Usage:
REM UploadToFTP [/L] FileToUpload
REM
REM Required Parameters:
REM  FileToUpload
REM      The file or file containing the list of files to be uploaded.
REM
REM Optional Parameters:
REM  /L  When supplied, the FileToUpload is read as a list of files to be uploaded.
REM      A list of files should be a plain text file which has a single file on each line.
REM      Files listed in this file must specify the full path and be quoted where appropriate.

SETLOCAL EnableExtensions

REM Connection information:
SET Server=
SET UserName=
SET Password=

REM ---- Do not modify anything below this line ----

SET Commands="%TEMP%SendToFTP_commands.txt"

REM FTP user name and password. No spaces after either.
ECHO %UserName%> %Commands%
ECHO %Password%>> %Commands%

REM FTP transfer settings.
ECHO binary >> %Commands%

IF /I {%1}=={/L} (
   REM Add file(s) to the list to be FTP'ed.
   FOR /F "usebackq tokens=*" %%I IN ("%~dpnx2") DO ECHO put %%I >> %Commands%
) ELSE (
   ECHO put "%~dpnx1" >> %Commands%
)

REM Close the FTP connection.
ECHO close  >> %Commands%
ECHO bye    >> %Commands%

REM Perform the FTP.
FTP -d -i -s:%Commands% %Server%

ECHO.
ECHO.

REM Clean up.
IF EXIST %Commands% DEL %Commands%

ENDLOCAL

Links

Download Upload to FTP Script from Sysadmin Geek

Jason Faulkner is a developer and IT professional who never has a hot cup of coffee far away. Interact with him on Google+

  • Published 09/8/10

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!