Automate FTP sessions with Windows

You’d like to upload some files from a PC running Windows via FTP to another server every now and then? It’s as easy as:

  • saving the appropriate FTP commands in a text file
  • using Windows’ built-in ftp command along with the text file
  • adding a task to the at scheduler.

Let’s have a look at this.

The upload script

First we’ll create a file that holds the FTP commands and then we’ll write a small batch script that uses the ftp command to execute the commands from the text file; have you ever read a sentence that uses the word command more often :-)?

This is what the text file (upload.ftp) may look like:

open example.com
username
password
cd another-directory
mput C:filesfile*.txt
bye

It will open a connection to the given host using the supplied username and password. Then it will change the working directory on the server to another-directory and finally upload some files. Adapt everything to your needs; if you’d like to use more FTP commands, this is a good starting point.

Next we’ll create a batch script that calls the ftp command using the text file that we’ve just created. Save the following into a batch file (upload.bat):

@echo off
@set logfile=upload.log
@ftp -i -v -s:"upload.ftp" > %logfile%
@del %logfile%

It creates a log file (upload.log) and deletes it after the ftp command is finished – if you’d like to keep the log file remove the last line from the script.

Now you have a batch file that you can click on and the files will be uploaded to the FTP server. Although this was pretty easy to setup is has a great impact: You don’t have to do this manually any more.

Adding Automation

While the script that you’ve just created is fully functional you may want to run it automatically every now and then. To do this, you’ll have to:

  • make sure that the script uses absolute path names
  • add a task to the at scheduler

First put the batch script and the text file into a directory of your choice. Add the path to this directory in front of log file’s name in line two of the batch script and to the text file containing the FTP commands in line three.

At last you can add a recurring task with at like so:

at 22:30 /every:M,T,W,Th,F,S,Su C:Scriptsupload.bat

In this case the script that I put under C:Scripts will run every day at 22:30, i.e. 10:30 pm.

Conclusion

As we’ve seen it’s pretty easy to automatically upload files to some FTP server with Windows. For example, this comes in handy if you’d like to backup some files to another server.

2 thoughts on “Automate FTP sessions with Windows”

  1. Hi Christian,

    Thanks for your tutorial…

    Do you have any idea why Im still getting the “unkown host” and “invalid command” in the log file ?

    I did all you wrote but I still get the same errors.

    Thanks again!

  2. Hello Hugo,
    it’s hard to tell exactly without more detailed information but “unkown host” indicates that the host you specified couldn’t be found and “invalid command” says that a certain command, e.g. the different statements sent to the FTP server, wasn’t correct. I recommend trying the individual commands in the command prompt first – this way you should be able to find the error.

Comments are closed.