unattended FTP transfer from website to local machine

parimp
parimp used Ask the Experts™
on
I am trying to copy the contents of a folder on our website to a specified local folder and it works if I have the command prompt open and paste it in there.

However when I try and run it as a batch file it comes up with:

331 Password required for userxxx:

Any ideas on how to get around this?


@echo off
ftp.exe -s:%0
open 119.x.x.x
usernamehere
passwordhere
cd httpdocs
cd site
cd csv
binary
prompt
lcd c:\mget
mget * -i 
mdelete *

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You need to put your username and password and other ftp commands into a script file like this

    username
    password
    etc....

then save it as say, SCRIPT.TXT

then you fire up your ftp session using the script file to log you onto the remote server and carry out the ftp commands. You fire it up something like this:

    FTP -S:script.txt 119.x.x.x

NOTE - things to remember

you can ONLY use ftp commands inside the script file because once you've logged in, you're nolonger in DOS so DOS commands won't wok.

Your last command in your script file will be something like QUIT or BYE or whatever the command is for your particular ftp session to close and pass control back to the OS.

You will require a batch file that's something like this:

    @echo off
    Rem - do what you need to do to prepare local files and folders etc in DOS
    CD c:\mget

    Rem - fire up your ftp client - DOS commands nolonger work
    FTP -s:script.ftp 119.x.x.x

    Rem - when the ftp session ends, you're back in DOS again
    EXIT

Commented:
Oops! in the example i gave i might have confused you by stating 'script.txt' in one place and 'script.ftp' in another... it was a typo...

You need to put your username and password and other ftp commands into a script file like this

    username
    password
    etc....

then save it as say, SCRIPT.TXT

then you fire up your ftp session using the script file to log you onto the remote server and carry out the ftp commands. You fire it up something like this:

    FTP -S:script.txt 119.x.x.x

NOTE - things to remember

you can ONLY use ftp commands inside the script file because once you've logged in, you're nolonger in DOS so DOS commands won't wok.

Your last command in your script file will be something like QUIT or BYE or whatever the command is for your particular ftp session to close and pass control back to the OS.

You will require a batch file that's something like this:

    @echo off
    Rem - do what you need to do to prepare local files and folders etc in DOS
    CD c:\mget

    Rem - fire up your ftp client - DOS commands nolonger work
    FTP -s:script.txt 119.x.x.x

    Rem - when the ftp session ends, you're back in DOS again
    EXIT

Commented:
I use SyncBack Freeware V3.2.19.0 for this saves a lot of time and will give you a lot more options like having an email alert.

http://www.2brightsparks.com/downloads.html

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
If you do not want to use a separate FTP script versus a single batch file, you can use code like below. However, the login and password stuff does not work with all FTP servers. Some allow for a more reliable LOGIN statement which provides both user and password.



@echo off
(echo usernamehere
 echo passwordhere
 echo cd httpdocs\site\csv
 echo binary
 echo lcd c:\mget
 echo mget * -i 
 echo mdelete *
 echo bye
)| ftp.exe -v -i 119.x.x.x

Open in new window

Commented:
Nice one Qlemo.

Here's another way of doing it. This batch file will first creat the SCRIPT.TXT file for you and then fire up your FTP client using the script it created.

NOTE: Don't forget to enter your own username ans password in the CREATE.SCRIPT section of the code below.


REM ------------------------------------
REM Main
REM -------------------------------------
@ECHO OFF
CALL create_script.txt
FTP -S:script.txt 119.x.x.x
DEL script.txt
EXIT /B


REM -------------------------------------
REM Create Script
REM -------------------------------------
:create_script.txt
  >script.txt ECHO usernamehere
>>script.txt ECHO passordhere
>>script.txt ECHO cd httpdocs
>>script.txt RCHO cd site
>>script.txt ECHO cd csv
>>script.txt ECHO binary
>>script.txt ECHO prompt
>>script.txt ECHO lcd c:\mget
>>script.txt ECHO mget * -I
>>script.txt ECHO mdelete *
>>script.txt ECHO bye
>>script.txt ECHO quit
EXIT /B
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
t0t0,
that would look much more pretty with

REM ------------------------------------
REM Main
REM -------------------------------------
@ECHO OFF
CALL create_script > script.txt
FTP -S:script.txt 119.x.x.x
DEL script.txt
EXIT /B
 
 
REM -------------------------------------
REM Create Script
REM -------------------------------------
:create_script.txt
ECHO usernamehere
ECHO passordhere
ECHO cd httpdocs
RCHO cd site
ECHO cd csv
ECHO binary
ECHO prompt
ECHO lcd c:\mget
ECHO mget * -I
ECHO mdelete *
ECHO bye
EXIT /B

Open in new window

Commented:
parimp

I hope your question has been answered. Qlemo has demonstrated some VERY useful techniques using redirection. His first comment is very compact and neat - I like it. His second comment (above) is brilliant.

I would urge you to accept Qlemo's solution because he deserves it. He has demonstrated awesome flair again and having learned something new is more important to me than points.


Qlemo

I've said this before and I'm going to say it again. You ARE the greatest! That looks absolutely awesome. Who would have thought you could redirect a CALL? It's nice. It's elegent. Again, and you know this is rare - I've learned something new. Thank you!
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Eeeek, I cut&pasted a typo of you, t0t0. I'm ashamed I didn't see that earlier. Of course the call statement has to be

CALL :create_script > script.txt

I agree with you, t0t0. I learned myself something trying to do that. Never try:

call :create_script | ftp

That does not work. Another quirk (or bug?) I have to note in my "Advanced DOS pitfalls" article as soon as I edit it again ...

Commented:
Oops! I didn't notice the typo - you're absolutely right! I hope this hasn't caused parimp any distress.

Yes, you need to include the ':' (colon) infront of the sub-routine name. It has to be:

   CALL :create_script.txt

Instead of:

   CALL create_script.txt

Thank you Qlemo for spotting that....

Author

Commented:
Hey t0t0 and Qlemo, many thanks for the great comments, with them I have now got it working, but going to look at the software option also suggested that sends an email. Best Regards!

Commented:
Well desreved!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial