Make a free auto get/put files schedule with FTP scripts in DOS environment

Challenge:

msdosI need to pull over a file from our FTP server every minute to the database server. I do not want to install any FTP schedule utilities on the database server. So I decided to use the FTP.exe comes with the Windows server OS. I brought up a DOS prompt, type FTP and hit Enter. I got some prompt screens, what I needed to do is to provide the remote server IP (host name) and user credential information. After I logged on, I then can use some UNIX like commands as LS and GET to download files. Great, it approved that we really do not need a fancy FTP client program.

But how can I make the above process automatically, since I could not put user logon credentials into a DOS batch file.

Research Results:

To overcome the challenge above, I need to come up an FTP “script” which can work together with FTP in DOS, since FTP.exe supports scripting with the “-s:” option.

For example, if your plain text FTP script file has a name of FTPSCRIPT.TXT (assume you put the same directory as you type your DOS command), then you can use the following command to execute FTP script:

ftp -s:FTPSCRIPT.TXT

The sample texts below can give an idea what you need to put into the script file:

open ftp.microsoft.com
anonymous
username@nowhere.com
cd Products
cd Windows
cd Windows95
cd CDRomExtras
cd AdministrationTools
cd ApplicationTools
binary
hash
lcd c:\
get envars.exe
bye

Additional Notes:

If you more than one FTP program or need to specify you need to use the one come with your OS, then you can put %windir%\system32 (%windir% in Windows 95) as the path of ftp.exe. for example:

%windir%\system32\ftp.exe -s:FTPSCRIPT.TXT

For more information about the command-line options for FTP, please read on:
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-w:buffer] [-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:buffer Overrides the default transfer buffer size of 4096.
host Specifies the host name or IP address of the remote
host to connect to.

Using the “-A” option logs you in with user and host name you
specified in your TCPIP setup. Which is why I prefer to specify
the login name and password in the script.

Here are all the local FTP commands (which you can see by
typing “help” at an FTP prompt:

! delete literal prompt send
? debug ls put status
append dir mdelete pwd trace
ascii disconnect mdir quit type
bell get mget quote user
binary glob mkdir recv verbose
bye hash mls remotehelp
cd help mput rename
close lcd open rmdir

Just type help (or ?) followed by the command you want a
description of.

For example:

help append

gives this response:

append Append to a file

Solutions:

OK, back to my original challenge. To allow the db server to get the file every minute, I can turn the FTP script into a batch file, and then create a Windows schedule to run this batch file every minute.

I named my batch file as AUTOFTP.bat and put the following text inside (use the above texts as the reference sample):

%windir%\system32\ftp.exe -s:”%f0″
goto done
open ftp.microsoft.com
anonymous
username@nowhere.com
cd Products
cd Windows
cd Windows95
cd CDRomExtras
cd AdministrationTools
cd ApplicationTools
binary
hash
lcd c:\
get envars.exe
bye
:done
@echo off
cls
exit

The AUTOFTP.bat file above servers two different purposes. 1), It can be called as a DOS patch file and run the FTP.exe command with script file, then use goto done to skip all FTP script codes after script file is called; 2), It can be called as the FTP script file by itself, and the FTP program will just return “Invalid command” and go on to the next line harmlessly when it hits the DOS batch file commands at the top and bottom in the file.

Reference website: http://www.ericphelps.com

Tags: , , , , ,

Related posts

6 Responses to “Make a free auto get/put files schedule with FTP scripts in DOS environment”


  1. 1 autoverleih

    Thanks to the article, it make the life easier and make the commenter come back again and again

    [ Reply ]

  2. 2 henry

    Let me understand this, thank you. I will always return here.

    [ Reply ]

  3. 3 Vangelis

    Thanks. But I like the file name to be variable such as get %1
    and not get envars.exe. Entering ‘get %1′ I have a respond ‘%1 does not exist.’

    [ Reply ]

  4. 4 Patricia

    Good post.

    [ Reply ]

  5. 5 rikki

    thanks for useful article. I like it. But I didin’t understand about ftp -s:FTPSCRIPT.TXT

    [ Reply ]

  1. 1 怎样制作自动下载/上传文件的FTP脚本文件 | 【超凡博俗 之 数码人生】

Leave a Reply