• ARTICLES
SEARCH

How-To Geek

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.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/13/06

Comments (46)

  1. Alex Le Dain

    Fsync allows you to only ftp upload changed and modified files. Check out http://www.eternitysoftware.com/ProductList/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/workspaces/workspace.aspx?id=cbef68e4-07e7-492a-b9a5-3902ae6a179c

  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

  9. StarChild

    Very useful,
    but what about uploading entire subdirs?

  10. toy

    thanks dude!

  11. ramesh

    Hi:
    I need help to get the latest file from the given directory and ftp it.
    Could you please help me on this.
    I can do this in UNIX environment and but still I haven’t experimented with Windows environment.

    requirement:
    Cd Directory
    Get the Latest file based on the condition
    fileup
    quit

    thank you in advance.
    Ramesh

  12. premchop

    Thanks,
    This small script was really helpful for what I was trying to do today!

  13. Ariel Rivera

    simple and great.
    Works for me.
    It’s in production @ Houston Tx ;)

  14. nabbed

    Can you work out how to find a list of files older than 7 days and then delete those using ftp in a batch file

  15. Chuck

    How would I script a transfer of one file or folder to multiple sites with different logins?

  16. Brian

    Does this batch work for all ftp system? Godaddy.com ftp server cannot recognize the password, and unable to login.

  17. Alberto Silva

    Saved my day, many thanks!!!

  18. John

    Thanks, Geek! Works like a charm!

  19. Barun Jha

    How could I add the Source file & Destination file.

  20. Mike

    OMG thanks got my FTP update solution working in 5 minutes.

  21. SCGrant327

    SWEET! Got it working and modded it to create a downloader also!

    Thanks Geek!

  22. larry

    This is brilliant. I have needed to tweek it and still having an issues. I get a response “Consider using PASV”, then could not create file.

    Any suggestions?
    thanks

  23. Gordon

    THANKS! This is really helpful!

  24. RAJARAM

    ftp with dos batch file is good

  25. Antonio

    If you want to apply this bat to the older file in a directory you can use the “forfiles.exe” windows utility.
    Check it out.

  26. joshua

    This program may be overkill for this thread but AutoMate allows for some pretty robust FTP automation without the need for batch files and scripts. http://www.networkautomation.com/automate/business-processes/automated-ftp-transfers/

  27. Martin

    Works fantastically, thank you, you saved my day :)

  28. Rob

    This works a treat and has was deployed within seconds.

  29. Stephen

    How could I make this upload a whole directory each time? maybe only the changed files of that specific directory? Copied from Todd

  30. Someone

    Hey man thanks for this!

  31. ALJ

    If you are using special characters in the password then you can escape them.

    For ‘%’ use a double percentage ‘%%’ and for other characters use a caret eg ‘&’ is ‘^&’

  32. Mikke

    My company used to do ftp transfers with command line ftp cmds as suggested here, but it didnt handle errors, like broken connections, too well. We tried to code in various error handling stuff, but it was impossible to make it work bulletproof (or at least for us). So we went with commercial app called batchsync ftp that handles transfers plus all error handling. It comes with command line tools to do transfers with subfolders. Liek we do our remote backups with that app. Its worth checking, as error handling code requires tons of work. Believe me.

  33. Gergő

    Great, works well, thank you!!

  34. ALBERT

    THANKS A LOT IT WORKS DAMN GOOOD.

  35. Ray

    Thanks a lot !!! You really saved my butt with this workaround.
    it works perfect !!!!

  36. nky

    Hi,
    I have one batch file which contents some statement and not I want to add ftp part means all statements like username password etc in same file. I don’t want to use another script file using -s:scritp but it works properly with this script.
    below are ftp commands I used:
    ftp machinename
    user
    pwd
    cd /someDir
    bin
    get abc.txt
    bye

    It not works properly…as waits for user and pwd on cmd prompt.
    Please let me know how can It work for me.

  37. Melbos

    Hi! This is great! But how about if I have a file which change the name according to the date, such as file_20110303.dat.

    How I process this file with a new name every day?

    Thank you a lot

  38. Jay

    No Geek there? No questions is answered!

  39. Vishwanatha M S

    Thanks a lot for giving these steps. i successfully automated the upload.

  40. David

    FTP’s default is to embed IP’s and port#’s inside the protocol which will cause most of its important functions (PUT and GET in particular) not to work when the client is behind a NAT firewall, as most people are these days. The solution to this is to have the server utilize PASV mode which instead uses a NAT friendly alternative to be used. If you have not been able to GET or PUT or seen a PASV related warning along with these problems, the solution is to add a line to the batch file (after you connect) that simply issues the command:

    LITERAL PASV

  41. DrGreenhamp

    Thanks a lot for these steps – all worked fine for me.
    Made my day ;)

  42. Kathy

    Thank you very much for this useful information!

  43. Steve

    Excellent article! Thank you so much!!!

  44. Rambo

    “Melbos

    Hi! This is great! But how about if I have a file which change the name according to the date, such as file_20110303.dat.

    How I process this file with a new name every day?

    Thank you a lot”

    Try this: echo put filename.txt filename_%date%.txt>>ftpcmd.dat

  45. Michael

    Works great!

    Thanks you are a life saver!!!

  46. Ron Anderson

    The advice at the top of this thread is superb. I’ve searched for days on finding a solution and everywhere I’ve looked the advise seemed either so complicated that even a computer couldn’t handle it or it was people who just wanted to show off their progamming ability and shrowded a very simple function with their “prowess”. Thanks for the simplicity, It’s good to see there are some truly sensible people still advising.

Enter Your Email Here to Get Access for Free:

Go check your email!