WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to FTP


Question:

I'm new to WinBatch and I am writing an FTP script that will perform the following in a DOS window:
  1. Start FTP
  2. Open(connect) to 30.30.3.45(server)
  3. Enter my UserID
  4. Enter my password
  5. Get a file
  6. Delete the file on my client once the download has completed
  7. Start over
Here is what I have so far:

Run("ftp.exe", "")
TimeDelay("1")
SendKeysTo("~ftp", "open 30.30.3.45")
SendKey("{ENTER}")
SendKey("UserID")
TimeDelay("2")
SendKey("Password")
TimeDelay("1")
SendKey("{ENTER}")
TimeDelay("2")
SendKeysTo("~ftp", "get file_name")
What function can I use to determine that the download has completed/finished so that I can then delete the file and start over?

Answer:

WinBatch cannot really see the contents of the dos window easily. Maybe instead of driving the dos window, use WinInet Extender Function to FTP the file.

Sample:

AddExtender("WWINT34i.DLL")
tophandle=iBegin(0,"","")
conhandle=iHostConnect(tophandle,"ftp.myserver.com",@FTP, "jane", "tarzan")
if conhandle==0
  message("ERROR","Unable to connect to host")
  exit
endif
prog=0
cancelbutton=1
var1=iFtpDialog(2,"Ftp session","Please wait while processing..",prog,cancelbutton)
Currdir=iFtpDirGet(conhandle)
getfile=iFtpGet(conhandle,"%Currdir%\myfile.zip","c:\windows\desktop\myfile.zip",0,@BINARY)
if getfile==0 then message("Oopps!",strcat("iFtpGet Failed check:",@CRLF," -Remote file path",@CRLF," -Overwrite Param(does local file already exist?)"))
iClose(conhandle)
iClose(tophandle)
Message("All","Done")
;Delete file here....
exit
Or if using the WinInet extender is not a possiblity and if FTP has to be conducted through a DOS window. You could accomplish the task with the use of the -s switch and creating a text file to read the FTP commands from. In other words create a file called 'ftp.txt' and specify what appears below.

Note: Replace 'username', 'password', 'directory' and 'filename.xxx' as appropriate for your needs. The BIN statement is only required if the file being retrieved is binary (an *.exe, *.dll, etc.). If an ASCII (text) file is being retrieved, you may omit the BIN statement.

OPEN 192.168.1.100
username
password
CD directory
BIN
GET filename.xxx
QUIT
To use the 'ftp.txt' in conjunction with the FTP command in DOS. All you need to do is type ftp -s:ftp.txt and press enter.

Now to quickly stuff the above into WinBatch.

coms=Environment("COMSPEC")
ftptxt=FileOpen("ftp.txt", "WRITE")
FileWrite(ftptxt, "OPEN 192.168.1.100")
FileWrite(ftptxt, "username") ;replace as needed
FileWrite(ftptxt, "password") ;replace as needed
FileWrite(ftptxt, "CD directory") ;replace as needed
FileWrite(ftptxt, "BIN") ;only needed if file is binary
FileWrite(ftptxt, "GET filename.xxx") ;replace as needed
FileWrite(ftptxt, "QUIT")
FileClose(ftptxt)
RunHideWait(coms, "/c ftp.exe -s:ftp.txt")
FileDelete('ftp.txt') ;delete the script
Exit

Article ID:   W16467
File Created: 2005:02:18:12:20:52
Last Updated: 2005:02:18:12:20:52