Thursday 27 August 2009

How to automate FTP uploads from the Windows Command Prompt

Since the start of Windows, they have included the ability to write and execute batch files to assist with either common, advanced, confusing or any other type of task.

As old as they are is exactly as useful as they are. Batch files are a great way to do something over and over again without typing all that needed code in the command line again and even though requiring some knowledge of batch scripting, doesn't exactly require much...

One of the things a batch file can do is automate the FTP uploads to a server of yours.
Here's one way of doing this;

This is something what you would put in your batch file...


@echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat SERVERNAME.COM
del ftpcmd.dat

What this batch file is doing is scripting the ftp utility using the -s option for the command line utility.

The batch file uses the “echo” command to send text to the ftp server as if you had typed it. In the middle of the file you can add extra commands, potentionally a change directory command:


echo cd /pathname/>>ftpcmd.dat


You will probably want to replace MyUserName, MyPassword and SERVERNAME.COM with your own variables unless of course, these are your own for your server.

In order to call this batch file, you will call the batchfile using the fileup.bat name that we gave it, and pass in the name of a file as the parameter. You don’t have to type the .bat part of the filename to make it work, either.

Like this:

> fileup FileToUpload.zip

Connected to ftp.myserver.com.
220 Microsoft FTP Service
ftp> user myusername
331 Password required for myusername.

230 User myusername logged in.
ftp> bin
200 Type set to I.
ftp> put FileToUpload.zip
200 PORT command successful.
150 Opening BINARY mode data connection for FileToUpload.zip
226 Transfer complete.
ftp: 106 bytes sent in 0.01Seconds 7.07Kbytes/sec.
ftp> quit

No comments:

Post a Comment