FTP with Batch File

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.
LVL 3
fjkaykr11Asked:
Who is Participating?
 
mcsweenConnect With a Mentor Sr. Network AdministratorCommented:
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
PWD password
GET \path\to\somefile.txt %userprofile%\Desktop\somefile.txt
PUT "c:\program files\somefile.txt" \path\to\somefile.txt
BYE

Open in new window

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

Open in new window

Here is a list of the methods the Microsoft FTP command supports
http://technet.microsoft.com/en-us/library/bb490684.aspx
0
 
fjkaykr11Author Commented:
@mcsween thanks for the reply.  
For the ftp -n -s:file.txt where do I specify the hostname of the ftp server ?
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
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

Open in new window

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
username
password
binary
put file1.zip
quit

Open in new window

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.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
fjkaykr11Author 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?
0
 
mcsweenConnect With a Mentor Sr. Network AdministratorCommented:
You can also call the command like this

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

Open in new window

0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
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
username
password
binary
put file1.zip
put file2.zip
put file3.zip
put file4.zip
quit

Open in new window


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.

Open in new window

0
 
fjkaykr11Author 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?
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
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
Connected to ftp.microsoft.akadns.net.
220 Microsoft FTP Service
User (ftp.microsoft.akadns.net:(none)): anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
ftp>

Open in new window


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

Open in new window


here is the sample run
C:\sample>ftp -s:myscript.txt
ftp> open ftp.microsoft.com
Connected to ftp.microsoft.akadns.net.
220 Microsoft FTP Service
User (ftp.microsoft.akadns.net:(none)):
331 Anonymous access allowed, send identity (e-mail name) as password.
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
ftp> quit
221 Thank you for using Microsoft products.

Open in new window

0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Have a look at my ftp scripts here:

http://scripts.dragon-it.co.uk/links/batch-ftp-scripting

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:

echo user username password

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 user username
echo password
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

Open in new window

0
 
fjkaykr11Author 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.
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
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.
0
 
fjkaykr11Author 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)

password:
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
what is the error you get?  

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

connection refused
unknown host somehost.com

Open in new window

0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Ok, did you try the methods shown, i.e.

echo user username
echo user username password

Steve
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
@dragon-it  That piping did not work for me either.  I get the same result of the prompt of the password.

sample
0
 
Jeff DarlingConnect With a Mentor Developer AnalystCommented:
@dragon-it it works if I put the password on the same line like this.

echo user username password
0
 
fjkaykr11Author 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
username
username
password
cd UploadDirectory
mput *
quit
0
 
Steve KnightIT ConsultancyCommented:
Yes, some servers work with one, some the other but they all should work with one of them.

Steve
0
All Courses

From novice to tech pro — start learning today.