Skip to main content

How to Automate FTP File Downloads in Batch Script

Automating the download of files from an FTP (File Transfer Protocol) server is a core task for data synchronization and automated updates. Whether you're pulling a daily CSV report from a vendor's server or downloading the latest build of an application, manual downloads are a bottleneck. While modern Windows includes curl, the built-in ftp client remains a reliable native option for legacy systems. By scripting the FTP client, you can ensure your data is always up-to-date without touching a single button.

This guide will explain how to automate FTP downloads using Batch.

Method 1: The "Scripted Commands" Pattern (Native FTP)

The Windows ftp.exe client performs best when you provide it with an external text file containing every command it needs to run.

@echo off
setlocal

set "FTPServer=ftp.example.com"
set "User=anonymous"
set "Pass=guest"
set "RemoteFile=data_export.zip"
set "LocalDest=C:\Downloads\data_export.zip"

set "FtpCmd=%temp%\download.ftp"

:: 1. Create the FTP command file
:: (Prefix redirection prevents trailing spaces in each line)
>"%FtpCmd%" echo open %FTPServer%
>>"%FtpCmd%" echo %User%
>>"%FtpCmd%" echo %Pass%
>>"%FtpCmd%" echo binary
>>"%FtpCmd%" echo get %RemoteFile% %LocalDest%
>>"%FtpCmd%" echo quit

:: 2. Launch the FTP client with the -s script flag
echo [ACTION] Downloading %RemoteFile% from %FTPServer%...
ftp -s:"%FtpCmd%"

:: 3. Clean up the command file (it contains credentials in plain text)
del /q "%FtpCmd%" 2>nul

:: 4. Verify the download
if exist "%LocalDest%" (
echo [SUCCESS] File downloaded to: %LocalDest%
) else (
echo [ERROR] Download failed.
)

endlocal
pause

Method 2: Modern One-Liner (CURL)

If you are on Windows 10 or 11, curl is the superior choice. It is faster, inherently more secure, and doesn't require creating temporary files on your drive.

@echo off
setlocal

set "URL=ftp://ftp.site.com/files/report.pdf"
set "Local=report.pdf"

echo [ACTION] Downloading using CURL...
curl -o "%Local%" -u "user:pass" "%URL%"

if %errorlevel% equ 0 (
if exist "%Local%" (
echo [OK] Downloaded successfully.
) else (
echo [ERROR] curl reported success but the file was not created.
)
) else (
echo [ERROR] Download failed.
)

endlocal
pause

Method 3: Managing Multiple Files (MGET)

If you need to download every .log file from a remote folder, use the mget (Multiple Get) command.

>>"%FtpCmd%" echo cd /logs
>>"%FtpCmd%" echo prompt
>>"%FtpCmd%" echo mget *.log
warning

The 'prompt' Command. When using mget, the FTP client will ask "Are you sure?" for every single file. You MUST include prompt in your script to toggle off these interactive confirmations, or your script will hang forever waiting for input.

How to Avoid Common Errors

Wrong Way: Downloading Zips in ASCII mode

The default mode for FTP is "ASCII," which is meant for text files. If you download a .zip, .exe, or .iso in ASCII mode, the file will be broken and unusable.

Correct Way: Always include the binary command in your FTP script before running get or mget.

Problem: Firewall Isolation

Many corporate firewalls block standard FTP (Port 21).

Solution: If your download fails to connect, try the -p flag for ftp (if supported) to use Passive Mode, or use curl which handles passive mode better by default.

Best Practices and Rules

1. Verification

Don't just run the command and assume it worked. Always use if exist "%Local%" to verify the file is actually on your drive before your script continues.

2. Credential Security

Remember that the FTP script file (%temp%\download.ftp) contains your password in plain text. Always use a temporary folder and delete the file immediately after the transfer. Likewise, curl -u "user:pass" exposes credentials in the process list; consider using a .netrc file or curl -K config_file for sensitive environments.

3. Log Redirection

Redirect the FTP client's output to a log file. This helps you troubleshoot "File not found" or "Login failed" errors that happen in the background. ftp -s:"%FtpCmd%" > ftp_log.txt

Conclusions

Automating FTP downloads is an essential skill for modern data management. By moving from manual downloads to scripted Batch file automation, you ensure that your data pipelines are consistent and efficient. Whether you use the classic native ftp tool or the modern speed of curl, you gain the ability to synchronize remote data with zero manual effort, keeping your local systems perfectly updated.