FTP from batch file..


By: subhash chandra
January 31, 2011

Hello Guys..

This will be my first post in programming forums. I am going to write something on how to use FTP from batch file.

Many times we have some files/folders that we wish to upload/download using FTP. The good thing is FTP supports scripting.

FTP.exe is not a native command line so using it in batch file is not as easy as using DIR, Echo, FOR, TIME,Date Etc.

Here are all the command-line options for FTP:

FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:buffer] [-A] [host]

Using the "-A" option logs you in with user and host name you specified in your TCPIP setup. Which is why I prefer to specify the login name and password in the script.

You can see all the local ftp commands by typing "help" at an FTP prompt:

Just type help (or ?) followed by the command you want a description on any command.

Coming back to Scripting, we can script FTP commands in  a text file and then call

ftp.exe -s:Textfile.txt

It will take one line from script, execute it and then take next till end.

What we need inside the Script is the Host name, Credentials commands and exit.

A basic example would be as follow..

Write below strings in Script.txt in the same directory from where we are going to run ftp.

open ftp.example.co.cc
username
password
cd foldername
cd Anothersubfoldername
hash
lcd c:\
get filename
bye

The script can be now called from command line

ftp -s:SCRIPT.TXT

To use this script from a batch file we have many ways . Iwill discuss few here.

Create a batch file which will extract the script and then call ftp.exe

@echo off
Echo open ftp.hostname.com >Script.txt
Echo username >>Script.txt
Echo password >>Script.txt
Echo cd foldername >>Script.txt
Echo cd Anothersubfoldername >>Script.txt
Echo hash >>Script.txt
Echo lcd c:\ >>Script.txt
Echo get filename >>Script.txt
Echo bye >>Script.txt

ftp.exe –s:script.txt
echo Command completed..Press any key to exit…
pause>nul

Another way :

Turn the script itself into a batch file J.

ftp.exe -s:%0
goto done
open ftp.hostname.com
username
password
cd foldername
cd Anothersubfoldername
hash
lcd c:\
get filename
bye
:done
@echo off & cls & exit

I just had to add 2 lines before the scipt and 2 lines after,now the script is all ready.

The great part is those same lines will turn ANY FTP script into a batch file! The DOS batch file will jump over the ftp script part, and the FTP program will just return "Invalid command" and go on to the next line harmlessly when it hits the DOS batch file commands.

Now then... Let's assume you don't want to embed your user name and password in the FTP batch file or script. Or maybe you want to pull the user name from somewhere else. Same problem. You're going to have to have your batch file create a separate script(using 1st method)  as it is needed. We'll start with a simple modification of the above sample code:

Either get the user and password variable values from standard input from console user using set /p

Set /p user=Enter the user name :
Set /p password=Enter the password

Or use batch file arguments to pass username and password. Like call the batch file like below:

Batch.bat user passwd

In the batch file, replace %user% with %1 and %password% with %2

Echo open ftp.hostname.com
>Script.txt
Echo %user% >>Script.txt
Echo %password% >>Script.txt
Echo cd foldername >>Script.txt
Echo cd Anothersubfoldername >>Script.txt
Echo hash >>Script.txt
Echo lcd c:\ >>Script.txt
Echo get filename >>Script.txt
Echo bye >>Script.txt
ftp.exe –s:script.txt
echo Command completed..Press any key to exit…
Pause>nul

I believe that should be enough for FTPing J. Do let me know your
thoughts/suggastions/comments or incase any help needed.

Thank You.
Subhash Chandra.


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question