Solved

.bat file to automatically download from FTP

Posted on 2010-08-16
45
873 Views
Last Modified: 2013-11-29
Hello,

I would like to create a .bat file that logs into our FTP goes to the specified directory, and downloads the file in there to a location on the local computer.  

Would it be possible to do this? I would like to automate the .bat file as a scheduled task.
0
Comment
Question by:printmedia
  • 18
  • 13
  • 7
  • +2
45 Comments
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
0
 
LVL 1

Expert Comment

by:sfranks
Comment Utility
Yes you can do this, however, I'd suggest a slightly different method.

Install cygwin and script this using cron, and wget.

Writing up a similar batch file is going to be quite a bit more tricky.

http://www.cygwin.com

If you want to go this path I can guide you through it.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
If you decide to use a better FTP client than the built in Windows one, that allows better scripting support, I'd recommend NCFTP, I have used it quite nicely in some batch files in the past.  Free.  And all parms are specified right on the command line.

http://www.ncftp.com/ncftp/

~bp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Easy!

NOTE, change the data supplied at the start of the code (the 6 SET statements) to suit your own requirements.



@ECHO OFF

SET File=filename.ext
SET LocalDirectory=c:\images
SET RemoteDirectory=images
SET UserID=YourUserID
SET Password=YourPassword
SET FtpAddress=ftp.address.co.uk

 >script.ftp ECHO %UserID%
>>script.ftp ECHO %Password%
>>script.ftp ECHO BINARY
>>script.ftp ECHO PROMPT
>>script.ftp ECHO LCD "%LocalDirectory%"
>>script.ftp ECHO CD "%RemoteDirectory%"
>>script.ftp ECHO MGET %File%
>>script.ftp ECHO bye

FTP -s:script.ftp %FtpAddress%
del script.ftp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Here's a shorter and easier way of doing it:


@ECHO OFF

 >script.ftp ECHO YourUserID
>>script.ftp ECHO YourPassword
>>script.ftp ECHO BINARY
>>script.ftp ECHO PROMPT
>>script.ftp ECHO LCD "YourLocalDirectory"
>>script.ftp ECHO CD "YourRemoteDirectory"
>>script.ftp ECHO MGET "YourFile"
>>script.ftp ECHO bye

FTP -s:script.ftp YourFtpAddress
del script.ftp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Actually, to get just a single file, you should use GET instead of MGET... So, replace the following line:

   >>script.ftp ECHO MGET "YourFile"

with:

   >>script.ftp ECHO GET "YourFile"
0
 

Author Comment

by:printmedia
Comment Utility
So the file would have the following in it:

@ECHO OFF

 >script.ftp ECHO YourUserID
>>script.ftp ECHO YourPassword
>>script.ftp ECHO BINARY
>>script.ftp ECHO PROMPT
>>script.ftp ECHO LCD "YourLocalDirectory"
>>script.ftp ECHO CD "YourRemoteDirectory"
>>script.ftp ECHO GET "YourFile"
>>script.ftp ECHO bye

FTP -s:script.ftp YourFtpAddress
del script.ftp

Exactly as it shows above, just obviously replacing with my info, correct?  Also the only place that I have to put the FTP server address would be at the end where is says "YourFtpAddress?"
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Yes. I forgot to say, copy and paste the code into Notepad and save it as MYFTP.BAT. Then, fire up a DOS session and enter the comment:

    MYFTP


The ONLY drawback to this method is that you have hard-coded your password into the batch file code. If this is an issue for you (where other people may have access to your computer) then the following variation to the code would be a safer bet:

   @echo off
   if "%~1"=="" (
      echo Specify a password at runtime ie, %~n0 ^<your password^>
      exit /b 1
   )
    >script.ftp ECHO YourUserID
   >>script.ftp ECHO %~1
   >>script.ftp ECHO BINARY
   >>script.ftp ECHO PROMPT
   >>script.ftp ECHO LCD "YourLocalDirectory"
   >>script.ftp ECHO CD "YourRemoteDirectory"
   >>script.ftp ECHO GET "YourFile"
   >>script.ftp ECHO BYE

   ftp -s:script.ftp YourFtpAddress
   del script.ftp


And assuming you've saved it as MYFTP.BAT again, this time you will need to specify your password when firing up the program - like this:

    MYFTP yourpassword


That way, your password is not compromised by a casual user.

Finally, as before, don't forget to place your own details in the code substituting the following value:

   YourUserID
   "YourLocalDirectory"
   "YourRemoteDirectory"
   "YourFile"
   YourFtpAddress


Easy as pie and chips !!
0
 

Author Comment

by:printmedia
Comment Utility
In the part where you enter the remote directory, how would you format the URL of the directory?  I currently have it like this (FileZilla did it this way automatically):

ftp://USER@ftp.server.com/public/images/

I'm guessing that's wrong since theres no need for the username to be incorporated in there, would it be ftp://ftp.server.com/folder/folder??
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Niether...Here's an example with a talk-through...

Suppose i want to download the file FILE.JPG from IMAGES\PHOTOS to C:\DOCUMENTS AND SETTINGS\PAUL\MY WEB\IMAGES then I would need the following (fictional) data:

   MyUserID                     paultomasi
   MyPassword               abcdefg
   MyLocalDirectory        %userprofile%\my documents\my web\images
   MyRemoteDirectory    images\photos
   MyFile                            file.jpg
   MyFtpAddress             ftp.pwp.blueyonder.co.uk

where %userprofile% is the system variable: C:\DOCUMENTS AND SETTINGS\PAUL


Therefore, my code would look something like this:

   @echo off
   if "%~1"=="" (
      echo Specify a password at runtime ie, %~n0 ^<your password^>
      exit /b 1
   )
    >script.ftp ECHO paultamasi
   >>script.ftp ECHO %~1
   >>script.ftp ECHO BINARY
   >>script.ftp ECHO PROMPT
   >>script.ftp ECHO LCD "%userprofile%\my documents\my web\images\"
   >>script.ftp ECHO CD "images\photos\"
   >>script.ftp ECHO GET "file.jpg"
   >>script.ftp ECHO BYE

   ftp -s:script.ftp ftp.pwp.blueyonder.co.uk
   del script.ftp


Finally, I would save the batch file as MYFTP.BAT and invoke it like this:

   MYFTP abcdefg


NOTE: Pay attention to where I have used double-quotes around the source and destination pathnames and the filename.
0
 

Author Comment

by:printmedia
Comment Utility
I dont mind if the password is hard coded to the script so I'm using the simple version of your code that you posted earlier.  When I try running it the command prompt stays there, no text displaying, just black.  

for the paths I have the following:

>>script.ftp ECHO LCD "C:\test"
>>script.ftp ECHO CD "public\images"

I made the LCD simple in that I just want it in the folder on the root of C so I hope that's ok.  
0
 

Author Comment

by:printmedia
Comment Utility
Actually, after leaving the cmd opened to see if I was just not giving it enough time, it comes back with the following error:

"The requested operation cannot be performed on a file with a user-mapped section opened."
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
I'm not familiar with that error message.

Is it possible you are trying to download a file from a remote server over-writing an existing file by the same name - and that the file you are a over-writing is in use by another program or process?

When you run FTP, DOS will pass control over to the FTP program and your blinking cursor will NOT return until FTP is exited by receiving the 'BYE' command.

To make sure though, just enter FTP as a command in DOS and then enter a single '?' to display FTP's help page. Confirm FTP is exited with the 'BYE' command. Enter 'BYE' to return to DOS.
0
 

Author Comment

by:printmedia
Comment Utility
Theres no over-writing going on.  I entered FTP as a command in DOS and the "?" to bring up help and exited back to DOS with the "BYE" command.  I tried turning off trend micro incase it was blocking it, but still nothing.
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
From http://www.codingforums.com/archive/index.php/t-6810.html there seems to be something that you could try:

>>"Cannot copy folders: The requested operation cannot be performed on a file with a user-mapped section opened"

Never heard of it. Maybe try a restart so that you are sure the files are not in use.

Also, can you copy the files somewhere else on your HD, and try to move them from there?

--------------------------------------------------------------------------------


WOOOOOO HOOOOOOO!!!

THANK YOU THANK YOU THANK YOU!!

I can't believe it was something that simple. For some reason it would not copy directly into that OE folder but when I created a folder on my desktop, copied from the cdrom into there and then from there into the OE folder.... viola! It worked!

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> "The requested operation cannot be performed on a file with a user-mapped section opened."

What was the last thing displayed to the screen right before this error?

~bp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Oops! Forgot to submit this comment before snoozing off at the keyboard last night.


Try that then...

Your 'LCD c:\test' does not lead me to suspect you have a share violation however, it might be worth trying it anyway.

First of all though. Make sure 'c:\test\' actually does exist before running the batch file.

If all is in order, try creating another folder and use that one instead. Make a folder named 'c:\ftpfiles\' and change you 'LCD' line to:

   LCD c:\ftpfiles\

Then try running the script again.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
prew... As far as I am aware, this has nothing to do with the FTP code I gave. The error messege relates to something totally seperate.

The ONLY way to suss this one out is to enter each FTP command manually and see what FTP reports back after each command.

Unfortunately, even if this messege is generated during an FTP session, it won't actually be displayed until FTP is exited and control is passed back to DOS.

There are other things that can be tried apart from manually FTPing - the names of locations and filenames can be changed. The file can be move manually first to ensure all works prior to attempting a batch approach.

Also, despite the error messege, does the file actually copy though?
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
printmedia

The problem you are having may relate to a numbet of issues - it is difficult to pinpoint which it is without trial-and-error.

Please follow these instructions.

   (1) fire up a DOS session (if you haven't already done so)...

   (2) enter the command:

            FTP your_remote_server

            where 'your_remote_server' is either an IP address or an FTP address ie, 'ftp.domain.co.uk'

   = you should now see "Connected to..." followed by your remote_server_name
   = you may also see other stuff displayed for your information

   (3) you may be prompted for a 'User' ID so enter your user_name

   (4) you may now be prompted for 'Password' so enter your password

   = if all goes well, you should now be logged onto your remote_server

   (5) enter the following command:

            LCD c:\test

   = look at the messege FTP displays. Did the command work ok?

   (6) enter the following command:

            CD public\images

   = again, look at the messege displyed by FTP. Did the command work ok?

   (7) enter the following command:

            DIR

   = look very carefully at the output.
   = firstly, didi the command work ok?
   = can you see the name of the file you want to download?

   (8) enter the following command:

            GET your_filename.extension

            where 'your_filename_extension' is the name of the file you want to download

   = did the command work ok? what is the messege FTP displays?

   (9) enter the following command:

            BYE

   = did the command owrk ok? did DOS display any messeges? if so, what?

   (10) enter the following command:

            DIR c:\test\

   = does the DIR command list your downloaded file?


Please confirm the results of the above manual tests and let us know your results before we can advise you further.

Thank you.


           

0
 

Author Comment

by:printmedia
Comment Utility
Alright I just did all the steps above, and they all worked. The file downloaded.

I logged in fine, so I'll skip to step 5.

5) Command worked ok.

6) Changed the directory to public/images

7) Command worked ok, I can see the name of the file i want to download.

8) Says Transfer complete

9) Says Goodbye!

10) CIR c:\test does show the file
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Okay... Going on what you've said, the following code automates exactly what you have done manually.

NOTE: You will need to edit this code to include your own details. These will be:

   - FTP Address
   - User ID
   - Password
   - Remote directory
   - Local directory
   - Filename


Here's the code:


   ::==============================
   :: Edit the following 6 'SET' statements
   ::==============================
   @echo off
   set FtpAddress=FtpAddress
   set UserID=Your_UserId
   set Password=Your_Password
   set Source=RemoteDirectory
   set Destination=LocalDirectory
   set Filename=Filename_to_download

   ::--------------------------------------------
   :: Create FTP script file
   ::--------------------------------------------
   (
      echo %UserID%
      echo %Password%
      echo CD "%Source%"
      echo LCD "%Destination%"
      echo GET "%Filename%"
      echo BYE
   
   )>script.ftp
   
   ::--------------------------------------------
   :: Download file
   ::--------------------------------------------
   ftp -s:script.ftp %FtpAddress%

   ::--------------------------------------------
   :: End
   ::--------------------------------------------
   del script.ftp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
BTW, when editing the first 6 lines of SET statements, ensure there are no spaces around the '=' sign nor at the end of your user-supplied info.

For example

   SET UserID=johndoe

   - do not include a space between 'UserID' and '='
   - do not include a space between '=' and 'johndoe'
   - ensure there are no spaces after 'johndoe'. Press [ENTER] key straight after the 'e' in 'johndoe'

This applies to ALL your SET statements as DOS can play tricks with you if you're not careful.

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:printmedia
Comment Utility
I copied your code exactly like this:  


   ::==============================
   :: Edit the following 6 'SET' statements
   ::==============================
   @echo off
   set FtpAddress=FtpAddress
   set UserID=Your_UserId
   set Password=Your_Password
   set Source=RemoteDirectory
   set Destination=LocalDirectory
   set Filename=Filename_to_download

   ::--------------------------------------------
   :: Create FTP script file
   ::--------------------------------------------
   (
      echo %UserID%
      echo %Password%
      echo CD "%Source%"
      echo LCD "%Destination%"
      echo GET "%Filename%"
      echo BYE
   
   )>script.ftp
   
   ::--------------------------------------------
   :: Download file
   ::--------------------------------------------
   ftp -s:script.ftp %FtpAddress%

   ::--------------------------------------------
   :: End
   ::--------------------------------------------
   del script.ftp

And edited all the info I had to in both sections, making sure of no spaces.  

Then I tried it like this:

   @echo off
   set FtpAddress=FtpAddress
   set UserID=Your_UserId
   set Password=Your_Password
   set Source=RemoteDirectory
   set Destination=LocalDirectory
   set Filename=Filename_to_download

 
   (
      echo %UserID%
      echo %Password%
      echo CD "%Source%"
      echo LCD "%Destination%"
      echo GET "%Filename%"
      echo BYE
   
   )>script.ftp
   

   ftp -s:script.ftp %FtpAddress%


   del script.ftp

And still nothing, the command prompt just stays open and black.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
Given all the trouble you are having, you might want to take a look at NCFTP, it can be as simple as:

ncftpget -u userid -p password -V host "c:\documents" "images/photos/file.jpg"

~bp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
prew... while your suggestion is very welcome, it has to be borne in mind that FTP is already part of the OS whereas, NCFTP is a third-party application.

There is obviously a problem trying to FTP a file. This should be a simple and effortless task. I, for one, am very curious as to why the asker cannot achieve a simple automated FTP.

I was about to suggest the asker takes note of all the messeges output by FTP in response to eacho of it's automated commands.

Only when we have examined those messeges can we be closer to fuguring out what's happening and why.
0
 

Author Comment

by:printmedia
Comment Utility
It doesn't output any messages just stays black, almost as if frozen.
0
 

Author Comment

by:printmedia
Comment Utility
By the way was this the correct format?


   ::==============================
   :: Edit the following 6 'SET' statements
   ::==============================
   @echo off
   set FtpAddress=FtpAddress
   set UserID=Your_UserId
   set Password=Your_Password
   set Source=RemoteDirectory
   set Destination=LocalDirectory
   set Filename=Filename_to_download

   ::--------------------------------------------
   :: Create FTP script file
   ::--------------------------------------------
   (
      echo %UserID%
      echo %Password%
      echo CD "%Source%"
      echo LCD "%Destination%"
      echo GET "%Filename%"
      echo BYE
   
   )>script.ftp
   
   ::--------------------------------------------
   :: Download file
   ::--------------------------------------------
   ftp -s:script.ftp %FtpAddress%

   ::--------------------------------------------
   :: End
   ::--------------------------------------------
   del script.ftp


Editing my information in the SET statements and the ECHO ones along with the last one in the "Download File" section
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 250 total points
Comment Utility
Taking prew's suggestion, it might be worthwhile trying NCFTP to see if the transfer will work anyway.

If it works then you can either stay with that, or continue to investigate what's causing your problem.

Please try this modified code which uses NCFTP:

NOTE: As before, edit the SET statements adding your own FTP details.


@echo off
set FtpAddress=FtpAddress
set UserID=Your_UserId
set Password=Your_Password
set Source=RemoteDirectory
set Destination=LocalDirectory
set Filename=File_to_download

ncftpget -u %UserID% -p %Password% -v %FtpAddress% "%Destination%" "%Source%\%Filename%"
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Seems like our comments crossed paths...
0
 

Author Comment

by:printmedia
Comment Utility
I include that bit of NcFTP code at the bottom of the code? All in the same .bat file?
0
 

Author Comment

by:printmedia
Comment Utility
That worked!!! I used the code with the NCFTP code at the bottom and it transferred the file quick and easily.
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Here something else to consider:

ftp -s:test.scr 11.11.11.11

However, if the FTP host implements automatic login, this command will not work. To turn off automatic login, use the -n switch in the command line as follows:
ftp -n -s:test.scr 11.11.11.11

A couple of other things of note.

You stated that you changed the SET, ECHO and Download File Location. The only ones you should have changed were the SET as the others all point to those.

Since running the FTP command in a command window everything works it would appear that it must have something to do with either the timing or the initial script. I ran the script up to the point of Download File Location and then looked at the script.ftp and it appeard correct so I would be inclined to believe that it is a timing issue in giving the responses OR related to the automatic login mentioned.

0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Well I was typing my comment when you answered with your two messages.  Based on your responses I would think that if you go back and ONLY change the SET to your information the code should work without the ncftp line at the bottom.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
you can find NCFTP at http://www.ncftp.com/download/
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
I agree with pony10us.

I DO ASSUME you ONLY made changes to the 6 SET commands AND NOTHING else in the original code I gave... Please confirm this is the case.

Or, as pony10us states, please try it again but ONLY change the 6 SET statements.

One thing to note is FTP should display stuff on the screen as it executes each command from within the 'script.ftp' file....

If this is NOT the case then you might need to issue the 'VERBOSE' command like this.

NOTE: 2 things you should do.

   - first, confirm whether or not this works
   - when you are back in DOS, enter the following command and confirm the details look fine

      TYPE c:\scriptv.tfp




   ::==============================
   :: Edit the following 6 'SET' statements
   ::==============================
   @echo off
   set FtpAddress=
   set UserID=
   set Password=
   set Source=public/images
   set Destination=c:\test
   set Filename=

   ::==============================
   :: Do not change anything below this line
   ::==============================
   ::--------------------------------------------
   :: Create FTP script file
   ::--------------------------------------------
   (
      echo VERBOSE
      echo %UserID%
      echo %Password%
      echo CD "%Source%"
      echo LCD "%Destination%"
      echo GET "%Filename%"
      echo BYE
   
   )>c:\scriptv.ftp
   
   ::--------------------------------------------
   :: Download file
   ::--------------------------------------------
   ftp -s:c:\scriptv.ftp %FtpAddress%

   ::--------------------------------------------
   :: End
   ::--------------------------------------------
   rem del c:\scriptv.ftp
0
 

Author Comment

by:printmedia
Comment Utility
It works doing this:  

@echo off
set FtpAddress=FtpAddress
set UserID=Your_UserId
set Password=Your_Password
set Source=RemoteDirectory
set Destination=LocalDirectory
set Filename=File_to_download

ncftpget -u %UserID% -p %Password% -v %FtpAddress% "%Destination%" "%Source%\%Filename%"

0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
That's good.

You must already have ncftp then. t0t0's script is generic so does not require a 3rd party product. Should you decide to port the script to another machine you should keep that in mind. Using your last script will require that ncftp be installed on the new machine as well.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> Using your last script will require that ncftp be installed on the new machine as well.

While this is true, one of the nice things about NCFTP is that is deployed by just bringing the EXE file to the machine.  So you have to bring a BAT file there anyway, this just requires that you bring the EXE for NCFTP there as well.

While it isn't built in, it's easy to deploy, and in my opinion if offers features the fairly basic built in FTP client doesn't offer.

~bp
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
Both are good points bill.  I was just putting that out as a caution to keep it in mind. Perhaps it might be an idea to include a comment in the batch file that it relies on having ncftp and steps to be taken should the batch file be moved to another machine.

I like to document my code as much as possible.

:)
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Agreed.

~bp
0
 

Author Comment

by:printmedia
Comment Utility
Yea i just tried it again using the code in t0t0's last post without the NCFTP and it wont work, so I'll stick to the NcFTP version, the exe is a small install anyways.

Thank you all for your help!
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
You're welcome, glad that worked out for you.

~bp
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Unfortunately for us boffins, we are no nearer in understanding why FTP failed to get a file however, I'll chalk this one up for now and see if this problem crops up again in the future.

Thank you

And thank you prew for NCTFP - another one to add to the toolbox.
0
 
LVL 26

Expert Comment

by:pony10us
Comment Utility
print - glad it is working for you. This was an interesting thread and as usual I learned some stuff as well.

t0t0 - I still think your script would work properly but I guess we will never find out.

bill - Thanks for ncftp. I have been looking at it since you mentioned it and it looks good.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
pony

i agree....
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now