Using Windows FTP scripts to automate file transfers

Automate file transfers with Windows FTP scripts. Execute commands through batch files or PowerShell to simplify workflows, save time, and minimize errors. Consider secure alternatives like FTPS or SFTP for production environments.
  1. Blog

Using Windows FTP scripts to automate file transfers

Before they discovered managed file transfer (MFT), many Microsoft-oriented customers relied on Windows File Transfer Protocol (FTP) scripts to automate their file transfer workflows. Windows FTP scripts enable you to string together a chain of commands in a file you can invoke through the command line, a batch file, Windows PowerShell, Windows Task Scheduler or a third-party automation tool.

For example, let’s say you run a manufacturing company and must regularly exchange electronic documents with a supplier. You may need to send purchase orders, requests for quotations, product specifications and other documents. You may also need to retrieve invoices, shipping documents and quality certificates.

While one of your IT staff can execute FTP commands manually in what is known as interactive mode FTP, doing so would be impractical for this particular use case. Not only would that option be tedious and time-consuming, but it would also be prone to errors.

If you have a Windows (for desktop) or Windows Server deployment and your supplier has an FTP server, you can instead create a Windows script file that would automatically upload or download files with your supplier’s FTP server. Putting all your FTP commands in a script file reduces or eliminates manual errors. It also saves time and improves efficiency.

Both Windows (for desktops) and Windows Server operating systems have a built-in FTP client. This client enables you to issue FTP commands through the command line or a script file.

Note: This discussion focuses solely on Windows FTP scripts for educational purposes. In this day and age, you really shouldn’t be using FTP in production environments. FTP is plagued with security issues. To ensure the security of your data transfers, consider shifting to secure file transfer protocols like FTP-over-SSL/TLS (FTPS) or SSH File Transfer Protocol (SFTP).

Creating a simple FTP script for the Windows command-line

An FTP script contains a series of FTP commands you typically issue during an interactive mode FTP session. Here’s a simple script that does the following:

  1. Logs on to an FTP server at IP address 192.168.100.101
  2. Enters "user1" as the username and "demo" as that username's corresponding password
  3. Changes to the remote directory “/ftp_uploads”
  4. Changes to the local directory "c:\files_to_upload"
  5. Uploads a local file with filename "samplefile.txt" to the server
  6. Exits

Equivalent FTP script:

open 192.168.100.101

user1

demo

cd /ftp_uploads

lcd c:\files_to_upload

put samplefile.txt

bye

ftp script_windows

The script showed here is simple and short. Many FTP scripts used in real-world production environments are much longer and certainly more sophisticated than that. If you consider that fact, you’ll understand why running FTP commands through scripts is much better than having someone execute those commands manually every time you need to perform the same file transfer workflow.

Going back to this script, if you wish to upload multiple files instead, you may replace the line on the script with the put command with the mput command. For example, mput file1.txt file2.txt file3.txt.

mput_windows

Or, if you wish to retrieve all txt files from that same directory on the remote server, you may replace the mput command with the mget command. For example, mget *.txt.

mget_windows

If you’re unfamiliar with FTP commands, consider reading this quick tutorial on the FTP command line.

You may write your FTP script using your favorite text editor and save it as an ASCII or plain text file. Then, you can run the script using the FTP command with the -s option. You must do this in the Windows command prompt. If you’re still on the Windows graphical user interface (GUI), run the cmd command from the Start menu or from the Run dialog box.

How to run FTP script in Windows using command prompt

Once you’re at the command prompt, you may navigate to the directory that contains the script and then run the script from there. Alternatively, you may run the “FTP -s” command from where you are and specify the exact file path. For example, if your script is in “c:\ftpscripts” you can execute the following commands at the command prompt.

This works if you change your working directory to the script’s directory: ftp -s:ftpscript.txt.

ftp_s_windows

This works if you run the script in any directory: ftp -s:c:\ftpscripts\ftpscript.txt.

ftp_s_c_windows

The command line output would more or less look like this:

C:\Users\Username>ftp -s:ftp_script.txt

ftp> open 192.168.100.101

Connected to 192.168.100.101.

220 Welcome to FTP server

User (192.168.100.101:(none)): user1

331 Please specify the password.

Password:

230 Login successful.

ftp> cd /ftp_uploads

250 Directory successfully changed.

ftp> lcd c:\files_to_upload

Local directory now C:\files_to_upload.

ftp> put samplefile.txt

200 PORT command successful.

150 Opening data connection for samplefile.txt.

226 Transfer complete.

ftp: 5 bytes sent in 0.00Seconds 5000.00Kbytes/sec.

ftp> bye

221 Goodbye.

commandline output1command_line output2

Running an FTP script from a batch file

Windows users who are comfortable with the command line often automate tasks using batch files. You can run a batch file as if it were an executable (exe) file. If the batch file is placed in a directory whose path is included in your PATH environment variable, you can run it from any directory.

In line with our discussion, you can use a batch script to create an FTP script on the fly and then execute the it at the end. The batch script code below, which we save in a batch file named “filedownload.bat,” demonstrates the concept just described.

@echo off

echo open 192.168.100.101> ftpgetscript.txt

echo user1>> ftpgetscript.txt

echo demo>> ftpgetscript.txt

echo get %1>> ftpgetscript.txt

echo quit>> ftpgetscript.txt

ftp -s: ftpgetscript.txt

batch script windows

The batch script above accepts an argument (%1), which you will specify when running the batch file later, and then passes the value to the FTP script file named ftpgetscript.txt. You will see this output when you execute the batch file to "download" samplefile.txt. You passed samplefile.txt as the argument to the batch file, which passed on to the FTP script.

C:\Users\Username>filedownload samplefile.txt

ftp> open 192.168.100.101

Connected to 192.168.100.101.

220 Welcome to FTP server

User (192.168.100.101:(none)): user1

331 Please specify the password.

Password:

230 Login successful.

ftp> get samplefile.txt

200 PORT command successful.

150 Opening data connection for samplefile.txt.

226 Transfer complete.

ftp: 5 bytes received in 0.00Seconds 5000.00Kbytes/sec.

ftp> quit

221 Goodbye.

ftp script_windows1ftp script windows2
If you prefer to run your batch file (and, consequently, the FTP script) on a pre-defined schedule, you may add your batch file to the Windows Task Scheduler. That's a separate configuration altogether and is beyond the scope of this tutorial.

windows_task_scheduler

The problem with Windows FTP scripts

Although Windows FTP scripts can help you automate FTP file transfers, they require extensive knowledge of FTP commands. Also, scripts are complex to manage, maintain and troubleshoot. And, if the person who wrote those scripts leaves your organization, you’ll be left high and dry.

Since it can be extremely difficult to trace the workflow and dependencies of a complex script, many IT admins who take over a predecessor’s FTP scripts prefer to discard them and start from scratch. The blog post “FTP automation without using scripts“ discusses these issues and the recommended solution for them in more detail.

The solution involves using JSCAPE MFT by Redwood, a managed file transfer solution that enables you to support practically any file transfer workflow. JSCAPE MFT supports FTP and several other file transfer protocols, such as FTPS, SFTP, AS2, OFTP, WebDAV etc.

More importantly, JSCAPE MFT greatly simplifies file transfer automation, allowing you to eliminate scripts. See it in action with a customized live prdouct demonstration, free. Request a demo here.

Some articles related to this

FTP Automation Without Using Scripts

Benefits of a Scheduled File Transfer and How To Set One Up

Auto Upload Files To A Remote Server Upon Arrival At A Local Directory

Forwarding Files From FTP To SFTP

How To Install A SFTP Server on Windows

12 File Transfer Protocols And The Businesses They're Best Suited For

10 Things You Can Do With An MFT Server - An Infographic

10 Ways to Make a Server to Server File Transfer Fit Enterprise Use