Solved

FTP with Batch File

Posted on 2012-12-27
18
1,197 Views
Last Modified: 2012-12-27
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.
0
Comment
Question by:fjkaykr11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 22

Accepted Solution

by:
mcsween earned 245 total points
ID: 38724818
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
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38724834
@mcsween thanks for the reply.  
For the ftp -n -s:file.txt where do I specify the hostname of the ftp server ?
0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38724837
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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 3

Author Comment

by:fjkaykr11
ID: 38724889
@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
 
LVL 22

Assisted Solution

by:mcsween
mcsween earned 245 total points
ID: 38724931
You can also call the command like this

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

Open in new window

0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38724935
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
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38724966
@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
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38725002
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 65 total points
ID: 38725012
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
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38725022
@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
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38725055
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
 
LVL 3

Author Comment

by:fjkaykr11
ID: 38725056
@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
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38725069
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 65 total points
ID: 38725071
Ok, did you try the methods shown, i.e.

echo user username
echo user username password

Steve
0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38725080
@dragon-it  That piping did not work for me either.  I get the same result of the prompt of the password.

sample
0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 190 total points
ID: 38725088
@dragon-it it works if I put the password on the same line like this.

echo user username password
0
 
LVL 3

Author Closing Comment

by:fjkaykr11
ID: 38725149
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38725161
Yes, some servers work with one, some the other but they all should work with one of them.

Steve
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question