Feedback

Codebox Software

Windows FTP Script

Tags:  
Published: 01 Apr 2007

This batch file can be used to automate various FTP operations, the version shown below will retrieve a single file from the server, however it is easy to modify the code to upload files instead, or to copy batches of files.

@echo off
SETLOCAL

REM ##################################
REM Change these parameters
set FTP_HOST=192.168.1.2
set FTP_USER=rob
set FTP_REMOTE_DIR=/
set FTP_REMOTE_FILE=test.txt
set FTP_LOCAL_DIR=c:\rob\tmp
set FTP_TRANSFER_MODE=ascii
REM ##################################
set /P FTP_PASSWD=FTP Password for %FTP_USER% @ %FTP_HOST%: 

set SCRIPT_FILE=%TEMP%\ftp.txt
(
    echo %FTP_USER%
    echo %FTP_PASSWD%
    echo %FTP_TRANSFER_MODE%
    echo lcd %FTP_LOCAL_DIR%
    echo cd  %FTP_REMOTE_DIR%
    echo get %FTP_REMOTE_FILE%
    echo bye
) > %SCRIPT_FILE%

ftp -s:%SCRIPT_FILE% %FTP_HOST%
del %SCRIPT_FILE%

ENDLOCAL

Notes

The script works by creating a temporary FTP script file, and running it using the ftp -s option of the Windows FTP client. The script collects the password for the FTP server interactively from the user, once it has been launched.

To use the script, first set the values of the 6 FTP_... variables according to your needs - the names are fairly self-explanatory, if in doubt then test the values using an interactive ftp prompt first. When the script is invoked it will prompt the user to enter an FTP password, this avoids the need to store the password within the script itself - if you aren't concerned about this, and prefer to allow the script to run without any user interaction, then replace the line beginning set /P FTP_PASSWD... with set FTP_PASSWD=your.password.here instead. If you wish to copy binary (ie non-text) files then you should set the FTP_TRANSFER_MODE variable to 'binary' instead of 'ascii'.

A few notes about security - the password that is entered may be recorded in your command-line history if the DOSKEY utility is active, to clear this history use the command DOSKEY /REINSTALL at the end of the script. The password, and other FTP details, are recorded in temporary variables which are removed from scope when the script terminates. The script file containing the FTP commands that were sent to the server (including the password) is also deleted when the script finishes. Note that if the execution of the batch file is interrupted, then there is a good chance that the FTP script file will not have been deleted - you should do this manually.

Send Feedback

Use this form to send feedback, if you want a reply please include your email address!