# FTP with Batch File

fjkaykr11 used Ask the Experts™
on
From a Windows XP computer, I am looking to create a batch file to automate an FTP Login and FTP copy of multiple files at one time up to an FTP Server.
I had posted this issue before a while back but didn't get around to testing the solutions offered.
Below is the link to one of the solutions offered.   I tried the open command and got an error.  The other option uses a .scr file along with the FTP -S and FTP -N
Any ideas on how I an acomplish all within the .bat file and not have to call a .scr file?     Thanks.

http://support.microsoft.com/kb/96269

open hostname.com
user
guest
guest

It also talks about ftp -s:test.scr
and using ftp -n option as well.
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Create a text file with your commands in it; the first two lines should be as follows, the rest are just a sample
USER userID
GET \path\to\somefile.txt %userprofile%\Desktop\somefile.txt
PUT "c:\program files\somefile.txt" \path\to\somefile.txt
BYE

Where UserID and password are your actual userid and password.  Then just call
ftp -n -s:somecommandfile.txt

Here is a list of the methods the Microsoft FTP command supports
http://technet.microsoft.com/en-us/library/bb490684.aspx

Commented:
@mcsween thanks for the reply.
For the ftp -n -s:file.txt where do I specify the hostname of the ftp server ?
Developer Analyst
Commented:
If your open command doesn't work, there are some possible reasons for that.

1. Is the FTP server is down?  Try connecting to the FTP manually.
2. Is your DNS resolving?  Try a nslookup command to whatever your hostname is.  If that doesn't work, then try by IP number instead of hostname.
3. Do you have a firewall blocking you?  You will need to setup a firewall rule to allow you an exception.

I've used the ftp -s option for years. it works well, but each put command is done in series, not parallel within a script.

If you want to open multiple connections so that you can do parallel transfers, you can use a batch program like this.

Create a command prompt batch file named DO.BAT
start ftp -s:test1.scr
start ftp -s:test2.scr
start ftp -s:test3.scr
start ftp -s:test4.scr

Your test1.scr will look something like you have posted from the microsoft site, but you will need the username and password.
open hostname.com
binary
put file1.zip
quit

Each of the scr files will have a single put command.  I would recommend limiting the number of parallel transfers.   remember that you need to code binary for any binary files like PKZIP files, excel files, etcetera.

When you run the DO.BAT the system will automatically open a new command window for each transfer and they will run in parallel.

Commented:
@jeffld To answer your questions about DNS and Firewall. That is not the issue I can connect fine from a command prompt using FTP IP Address and then typing in the user and password.   I am confused about your parallel transfers .scr reference?  Are you saying I can't do the other method, ftp -n -s:somecommandfile.txt  in Windows?
Commented:
You can also call the command like this

ftp -n -s:somecommandfile.txt ftp.somehost.com

Developer Analyst
Commented:
you can put all your transfers into one script file, but they will run one after the other.  In series.

In your question you specifically asked for "FTP copy of multiple files at one time up to an FTP Server".

If you don't care if your files run in series, then you can just use one script file.

open hostname.com
binary
put file1.zip
put file2.zip
put file3.zip
put file4.zip
quit


I've never used the -n parameter.

If you want to see the parameters for command line ftp just type ftp --help

C:\sample>ftp --help

Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.

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

-v             Suppresses display of remote server responses.
-n             Suppresses auto-login upon initial connection.
-i             Turns off interactive prompting during multiple file
transfers.
-d             Enables debugging.
-g             Disables filename globbing (see GLOB command).
-s:filename    Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a             Use any local interface when binding data connection.
-A             login as anonymous.
-w:buffersize  Overrides the default transfer buffer size of 4096.
host           Specifies the host name or IP address of the remote
host to connect to.

Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.


Commented:
@jeffld the open command doesn't work on my system, I get an error not recognized. I am aware of the help option. thanks.

@mcsween when I tried to follow your steps, I get an error invalid command for the login username, any ideas?
Developer Analyst
Commented:
Does the open work manually?

Here is an example where I connect manually and login using anonymous login.

I'm using ftp.microsoft.com as an example

C:\sample>ftp
ftp> open ftp.microsoft.com
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
230 User logged in.
ftp>


Here it is using a script

here is the script.  with anonymous login, anonymous is the user and any email address is the password.
open ftp.microsoft.com
anonymous
test@yahoo.com
quit


here is the sample run
C:\sample>ftp -s:myscript.txt
ftp> open ftp.microsoft.com
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
230 User logged in.
ftp> quit
221 Thank you for using Microsoft products.

IT Consultancy
Commented:
Have a look at my ftp scripts here:

That explains some of the different ways it needs, I prefer this method generally which keeps it all in one file like you wanted.  You might need username password  all one one line instead as:

and use the cd / lcd lines as you wish or just remove them if not needed.

hth

Steve

@echo off
(echo open 110.110.110.110
echo cd /destination/path
echo lcd C:\sourcepath
echo bin
echo put file1.zip
echo put file2.zip
echo put file3.zip
echo put file4.zip
echo quit
) | ftp -n -i


Commented:
@jeffld. I think I am missing something here.
If I type in open ftpserver ip I get an error.
Don't I need to type FTP first?
like
FTP Open ip address ?
If I do this manually and I type FTP, then I get an FTP prompt, then I can type in
Open FTP Server ok.  But I still am lost at how to accomplish this within a Batch File.
Developer Analyst
Commented:
Once you are at the FTP prompt, then all you do is send FTP commands.

if you type ftp and the command prompt and press enter, that gets you to the ftp prompt., then you can issue ftp commands.

The nice thing about -s parameter using a script file is that you can just have all your ftp commands, the put or get commands in a text file and it executes.

Commented:
@dragon-it thanks for the info.  When I tried your method the command prompt pops up and is stuck waiting for an input of the password (like below)

Developer Analyst
Commented:
what is the error you get?

Here are two of the common errors I've seen.

connection refused
unknown host somehost.com

IT Consultancy
Commented:
Ok, did you try the methods shown, i.e.

Steve
Developer Analyst
Commented:
@dragon-it  That piping did not work for me either.  I get the same result of the prompt of the password.

Developer Analyst
Commented:
@dragon-it it works if I put the password on the same line like this.

Commented:
I was able to get this done finally with adjusting @mcsween's method (see below for what worked).  Thanks.
FTP Batch File Command: ftp -i -n -s:file.txt FTPServerIP

file.txt file info