Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

How to automate FTP uploads from the Windows Command Line

Windows has included batch files since before it existed… batch files are really old! Old or not, I still find myself frequently creating batch files to help me automate common tasks. One common task is uploading files to a remote FTP server. Here's the way that I got around it.

First, you will have to create a file called fileup.bat in your windows directory, or at least inside some directory included in your path. You can use the "path" command to see what the current path is.

Inside the batch file, you will want to paste the following:

@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

You will want to replace the MyUserName, MyPassword and SERVERNAME.COM with the correct values for your ftp server. 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

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.

Example:

> 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

And that's all there is to it. Now your file should be sitting on the remote server.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 09/13/06 and tagged with: Windows, Command Line

Comments (8)

  1. Alex Le Dain

    Fsync allows you to only ftp upload changed and modified files. Check out http://www.eternitysoftware.co.....Fsync.html

  2. The Geek

    Alex,

    Thanks for the link… There are a lot of commercial products out there that allow syncing via ftp, but your's does seem fairly inexpensive, so I don't mind you posting here.

    There's also a freeware ftp sync commandline utility that can be found here:
    http://www.gotdotnet.com/works.....02ae6a179c

  3. Greg Stearns

    I think the point of all of this is to add ftp functionality easily to a batch script so you can have it doing other things. Besides, here's how to only upload archive marked files. You can of course change the attributes list as fit accoridng to dir /?

    ftpbackup.bat | dir * /a:a /b

  4. Todd

    How would I make this upload a whole directory each time? maybe only the changed files of that specific directory..

  5. zac krebs

    I used the concept of this script to automate a download from a product inventory provider. Thanks!

  6. rye

    Just a quick question… How do you trap/catch errors (such as incorrect credentials, unavailable server, etc.) from a FTP batch?

  7. Alain

    This works nice, but many servers give a timeout if file size >1Mb. If somebody knows which parameter to add to the script, it would be great.

  8. Rudi

    Below is a lin to a FTPSync utility. Works great.

    http://www.cyberkiko.com/FTPSync.aspx


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.