Using Windows FTP Scripts To Automate File Transfers
To run an FTP script in Windows, use your favorite text editor to create the script and save it in a text file, e.g., ftpscript.txt. To run the script, you execute the FTP command with the -s option. For example, ftp -s:ftpscript.txt.
Overview
Windows FTP scripts enable you to put together a chain of commands in a file that you can call into play when needed. Scripts can come in handy when you want to automate file transfer processes. In this introductory post, we explain what FTP scripts are, why you'll want to use them, and how to create simple Windows-based scripts to upload or download files from an FTP server.
In a previous article, we taught you how to execute FTP commands in the terminal. The examples we included in that tutorial were all done in interactive mode. Meaning they all required you to enter commands into the command line each time you wanted to do something during an FTP session, e.g., login to a server, display a directory listing, upload files, download files, change a directory, and so on.
Interactive mode is sufficient for ad hoc purposes. But if you need to transfer files on a regular basis, e.g. as part of a B2B transaction or data exchange, then interactive mode is no longer practical. You'll be much more efficient if you can automate some parts of the process, and the way to do that would be to write FTP scripts.
Note: FTP is no longer recommended for transferring sensitive files. Learn why in the article Countering Packet Sniffers Using Encrypted FTP.
Simple FTP script for the Windows command line
An FTP script consists of the same commands that you usually issue in an interactive session, except that the commands are entered into a file. Let's take a look at a simple script that:
- Logs in to a FTP server at 192.168.100.101;
- Enters "user1" as the username and "demo" as that username's corresponding password;
- Changes to the local directory "c:\ftpuploads";
- Uploads the file "samplefile.txt" to the server; and
- Exits
Here's the script for that:
open 192.168.100.101
user1
demo
lcd c:\ftpuploads
put samplefile.txt
quit
Notice how we've simply entered the same commands you'd normally use in interactive mode. You can use your favorite text editor to create the script and save it in a text file, e.g. ftpscript.txt. To run the script, you just execute the FTP command with the -s option. For example,
ftp -s:ftpscript.txt .
Here's what it looked like when I ran that command on my Windows command prompt:
So how different is this from interactive mode? Well, the BIG difference is that you no longer have to input the same values and FTP upload commands over and over every time you need to upload the same file to the same FTP server (there are certainly sophisticated scripts that can do more than that).
Most B2B file transfers are quite repetitive. Practically the same file transfers are carried out periodically. Why assign someone (who can probably do more productive tasks) to do that repetitive task when you can just call a script?
In Windows, scripts usually work with batch files. You may call that command we showed earlier from a batch file like this:
If the batch file (e.g. fileupload.bat) is located in a directory whose path is included in your PATH environment variable, then you'll be able to run that batch file as a command prompt executable file from any directory.
Here are the contents of a slightly more complicated Windows batch file (which we name filedownload.bat) that creates an FTP script on the fly and then executes it:
@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
This batch file accepts an argument (%1) and passes the value to the script. Here's how it looked like when I executed the batch file to "download" the file named samplefile.txt. samplefile.txt is the argument we passed to the batch file, which in turn passed it on to the FTP script.
If you want to run this batch file (and, consequently, the FTP script) on a pre-defined schedule, you'll have to add the batch file to your Windows Task Scheduler. That's a separate configuration altogether and is beyond the scope of this blog post.
Looking for a more secure and efficient way to manage your file transfers? Discover how JSCAPE MFT Server can automate and secure your data exchange processes. Schedule your free demo today and take the first step towards a more reliable file transfer solution
Need a secure file transfer solution?
Download a free JSCAPE MFT Server Trial now.
JSCAPE also provides broad functionality to help simplify and optimize your file transfer environment, including data loss protection, caching for HTTP/S content, and the ability to connect to virtually any web server with JSCAPE's REST API.
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