How to Start an FTP Transfer Script in a Batch Script
Automating the transfer of files using the File Transfer Protocol (FTP) is a classic scripting task. You might need to upload daily log files to a central server, download updated data files, or synchronize resources with a remote machine. While Windows has a built-in FTP client (ftp.exe), it is an interactive program, meaning it's designed for a user to type commands one by one.
This guide will teach you the standard method for automating ftp.exe from a batch script by creating a separate command script. You will learn how to write the necessary FTP commands, how to have your batch file dynamically generate this script, and how to call ftp.exe in a non-interactive mode to execute it.
CRITICAL SECURITY WARNING: The standard FTP protocol is not secure. It transmits your username, password, and all data in plain text over the network. Anyone with a network sniffer can intercept your credentials. This method should only be used on a trusted, private network or for accessing public, anonymous FTP servers. For secure transfers, use a command-line tool that supports SFTP or FTPS, such as WinSCP or the native sftp.exe included with modern Windows.
The Challenge: ftp.exe is an Interactive Program
You cannot simply pipe commands to ftp.exe like you can with other tools. A command like ECHO user myuser | ftp ftp.example.com will not work. Once ftp.exe starts, it enters its own interactive shell, and the parent batch script loses control until it exits.
The Core Method: Using a Command Script with the -s Switch
The solution is the -s command-line switch. This switch tells ftp.exe to read and execute commands from a specified text file instead of waiting for a user to type them.
Syntax:ftp -s:CommandScript.txt
-s:CommandScript.txt: The script switch, followed by a colon and the name of the text file containing the FTP commands.
The Two-File Approach Explained
To automate an FTP transfer, your solution will always involve two files:
- The Batch Script (
.bat): This is the main controller. Its job is to prepare the environment and then callftp.exewith the-sswitch. A robust script will often dynamically create the command script. - The FTP Command Script (
.txt): This is a simple text file that contains the exact sequence of commands you would have typed into the interactive FTP client, one command per line.
Basic Example: A Script to Download a File
This example downloads a file named daily_report.txt from a remote server.
@ECHO OFF
SET "FTPServer=ftp.example.com"
SET "FTPUser=anonymous"
SET "FTPPass=user@example.com"
SET "FileToDownload=daily_report.txt"
SET "CommandScript=%TEMP%\ftp_commands.txt"
ECHO --- Preparing FTP Download ---
ECHO.
REM --- Step 1: Create the FTP command script dynamically ---
ECHO open %FTPServer% > "%CommandScript%"
ECHO %FTPUser% >> "%CommandScript%"
ECHO %FTPPass% >> "%CommandScript%"
ECHO get %FileToDownload% >> "%CommandScript%"
ECHO quit >> "%CommandScript%"
ECHO Running FTP transfer...
REM --- Step 2: Execute ftp.exe with the command script ---
ftp -s:"%CommandScript%"
ECHO.
ECHO --- Step 3: Clean up the temporary command script ---
DEL "%CommandScript%"
ECHO --- Download complete ---
The batch script above would create the following text file ftp_commands.txt:
open ftp.example.com
anonymous
user@example.com
get daily_report.txt
quit
Another Example: A Script to Upload a File
The logic is identical, but you use the put command instead of get.
FTP Commands for an Upload
open ftp.example.com
MyUser
MyPassword
put "C:\Logs\local_log.txt"
quit
put: The command to upload a file.
Common Pitfalls and How to Solve Them
-
Plain-Text Passwords: This is the biggest risk. Storing a password in plain text in a script is a major security vulnerability. Solution: Acknowledge the risk and use this method only on trusted networks or with anonymous FTP servers. For secure environments, use SFTP.
-
Firewall Issues: FTP uses port 21 for commands but uses other, dynamic ports for the data transfer. Firewalls, especially on the client side, can block these data connections. Solution: This is an environmental issue. You may need to use "passive mode" by adding the command
literal pasvafter logging in, or ensure the necessary firewall ports are open. -
Error Handling:
ftp.exeis very basic. It does not set the%ERRORLEVEL%in a reliable way to indicate if a transfer succeeded or failed. A login failure or a "file not found" error will not stop the batch script from continuing. Solution: You can redirect the output of theftpcommand to a log file (ftp -s:commands.txt > ftp.log) and then usefindstrto search the log for specific error messages like "550 File not found".
Practical Example: An Automated Log File Upload Script
This script prepares a log file and uploads it to a backup FTP server.
@ECHO OFF
SETLOCAL
REM --- Configuration ---
SET "FTPServer=backupftp.internal.lan"
SET "FTPUser=log_uploader"
SET "FTPPass=S3cureP@ss"
SET "LogFile=C:\AppData\Logs\app_2023-10-27.log"
SET "CommandScript=%TEMP%\upload.txt"
ECHO --- Log File Uploader ---
ECHO.
IF NOT EXIST "%LogFile%" (
ECHO [ERROR] Log file not found. Aborting.
GOTO :End
)
ECHO Preparing to upload "%LogFile%"...
REM --- Create the FTP command script ---
(
ECHO open %FTPServer%
ECHO %FTPUser%
ECHO %FTPPass%
ECHO binary
ECHO put "%LogFile%"
ECHO quit
) > "%CommandScript%"
REM The 'binary' command is good practice to prevent file corruption.
ECHO Connecting and uploading...
ftp -s:"%CommandScript%"
DEL "%CommandScript%"
ECHO.
ECHO --- Upload complete ---
:End
ENDLOCAL
Conclusion
Automating FTP transfers from a batch script is a classic task that relies on a two-file system.
- The core command is
ftp.exe -s:CommandFile.txt, which runsftpin a non-interactive mode. - Your batch script (
.bat) acts as the controller. The best practice is for it to dynamicallyECHOthe commands into a temporary command file. - The command script (
.txt) contains the simple, one-per-line commands that you would normally type into an FTP client (open,user,pass,put,get,quit). - Always be aware of the major security risk of using standard FTP, which transmits your credentials and data in plain text.