How to Connect FTP With Batch Script

MD Aminul Islam Feb 02, 2024
How to Connect FTP With Batch Script

The FTP (File Transfer Protocol) is a standard protocol built on a client-server used to transfer files from one computer to another.

In this article, we will see how we can create an FTP script in Batch and connect with the FTP server. Also, we will see necessary examples and explanations to make the topic easier to understand.

Connect FTP With Batch Script

Before we start, we need to take this important note. To embed the FTP script in the Batch script, you need to add the line @ftp -i -s:"%~f0"&GOTO:EOF at the beginning of the FTP script.

Here, "%~f0" is used to resolve the name of the running Batch file, and GOTO: EOF is used to close the Batch script.

Example 1:

Below is a simple FTP script written in Batch; this is the most basic concept. You need to provide your web address, username, and password on the script.

Let’s go through the code of our example:

@FTP -i -s:"%~f0"&GOTO:EOF
OPEN example.com
username
password
!:--- YOUR FTP COMMANDS START FROM HERE ---
LCD c:\MyLocalDirectory
CD  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye

Example 2:

This is an example of a classic FTP script, which is mostly similar to the previous one. Let’s see the code of our example:

open example.com
username
password
!:--- YOUR FTP COMMANDS START FROM HERE ---
lcd c:\MyLocalDirectory
cd  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye

Example 3:

This is an example of an FTP script that allows you to connect with FTP automatically. The code for this example will look like the below:

@FTP -i -s:"%~f0"&GOTO:EOF
OPEN example.com
username
password
PWD

For all examples, you need to provide your own username and password on the script before executing it.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn