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

Sample Code

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

Sample Code Showing How a Pair of Machines can Transfer a File

Keywords: 	  serial extender file transfer

These are two pairs of scripts so that a pair of machines can transfer a file via a WinBatch script.

The "A" files have the modem that dials also sending the file and the modem that answers receives the file.

The "B" files have the modem that dials receiving the file and the modem that answers sending the file.

The "A" files are used when a central computer wants to update a bunch of remote sites with new files. It is dialing so that the remote sites do not conflict with each other trying to dial in.

The "B" files are used when a central computer wants to get a file from a bunch of remote sites.


A-SerialModemAnswerRecvfile.wbt

; Setup program, load extenders, turn on debugging, open com port
DebugTrace(@on,"revctrace.txt")
BoxOpen("Modem Receive File",1)
AddExtender("wwser34I.dll")
ComPort="COM1"
BoxText("Before pComOpen")
port=pComOpen(ComPort,0,38400,"8N1","RTSRTS")
pCaptureOn("recvcapture.html",0)


; Define target directory
TargetDir="c:\temp\"



; Set serial timeouts to 10 seconds
pTimeOut(port,10000)

; Wait for a call
BoxText("Waiting for Call")
pModemAnsCall(port,1,1)


; Send "Hello".  Make sure we get s "2U2" response.  Try until it works
BoxText("Get processes in sync here")
flag=0
while flag == 0
   pPutString(port,"Hello")
   flag=pWaitFor(port,"2U2"," ",0, 3000)
endwhile


; Get computername of sending computer.
BoxText("Getting remote computername")
pPutString(port,"What is your computername?%@CRLF%")
computername=pGetLine(port,200)
BoxText("Got Computername = %computername%")

;Get remote file name
pPutString(port,"What file are you sending?%@CRLF%")
BoxText("Waiting for filename")
FileNameRecievedAs=pGetLine(port,200)
BoxText("Got Filename = %FileNameRecievedAs% %@CRLF% Waiting for file")


;Receive file sent
pRecvFile(port,"ZMODEM",TargetDir,1,1)

;Map file name to target directory
FileNameRecievedAs=FileMapName(FileNameRecievedAs,Strcat(TargetDir,"*.*"))

;Make file name to rename received file as
FileNameToUse=strcat(TargetDir,computername,".txt")

;Rename file to desired name.  ZMODEM uses senders file name
FileMove(FileNameRecievedAs,FileNameToUse,0)


;Send byebye message to tell other script to hangup
BoxText("Print byebye message")
pPutLine(port,"ByeBye")

BoxText("Hanging up")
pModemControl(port,0,0)
pModemHangup(port)
BoxText("Closing Port")
pComClose(port,0)
Message("All","Done")

A-SerialModemDialSendfile.wbt

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  Tricky bits:
;  1) The pWaitFor's must include the CRLF on the end of the 
;     strings send, or else they will be received as part of
;     a pGetLine later....this confuses the issue
;
;  2) Because of timing problems, there is code to make sure
;     the two computers are in sync  (the Hello 2U2 stuff)
;     to force synchronization of the two computers.
;
;  3) There is not much error checking in this script
;
;  4) ZMODEM send the file with the base filename it was on
;     the sending system.  This script will figure that out
;     and rename the file
;
;  5) Need to set pTimeOut to *something* to allow time
;     for data to transfer
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Setup program, load extenders, turn on debugging, open com port
DebugTrace(@on,"sendtrace.txt")
BoxOpen("Modem Send File","")
AddExtender("wwser34I.dll")
ComPort="COM2"
BoxText("Opening Port")
port=pComOpen(ComPort,0,38400,"8N1","RTSRTS")
pCaptureOn("sendcapture.html",0)

;Define filename to send
FileToSend="c:\config.sys"


; Set serial timeouts to 10 seconds
pTimeOut(port,10000)

; Dial and connect to remote computer
BoxText("Dialing")
pModemDial(port,1,"9,,9355518")
BoxText("Connecting")
pModemConnect(port)



;Wait for a "Hello" message.  Keep trying until we get a "Hello"
BoxText("Get processes in sync here")
flag=0
while flag == 0
   flag = pWaitfor(port,"Hello"," ",0,3000)
endwhile

;We received a "Hello".  Send a "2U2" to confirm receipt.
pPutLine(port,"2U2")



; Wait for the word "computername?" followed by a CRLF
BoxText("waiting for computername prompt")
pWaitFor(port,"computername?%@CRLF%"," ",0,10000)

;Assume it was received.  Get local computer name and send it
sysinfo=WinSysInfo()
computername=ItemExtract(1,sysinfo,@tab)
BoxText("Sending computername %computername%")
pPutLine(port,computername)

;Wait for the word "sending?" followed by a CRLF
BoxText("waiting for filename prompt")
pWaitFor(port,"sending?%@CRLF%"," ",0,10000)

;Assume it was received.  Send other computer the filename we are sending
BoxText("Sending filename %FileToSend%")
pPutLine(port,FileToSend)

; Now send the file itself
pSendFile(port,"ZMODEM",FileToSend,0,1)

; Wait for ByeBye message from remote computer, then hangup
BoxText("Waiting for ByeBye prompt")
pWaitFor(port,"ByeBye"," ",0,10000)
BoxText("Hanging up")
pModemHangup(port)
BoxText("Closing port")
pComClose(port,0)
Message("All","Done")

B-SerialModemAnswerSendfile.wbt

;modem answer send file
; Setup program, load extenders, turn on debugging, open com port
DebugTrace(@on,"sendxtrace.txt")
BoxOpen("Modem Receive File",1)
AddExtender("wwser34I.dll")
ComPort="COM1"
BoxText("Before pComOpen")
port=pComOpen(ComPort,0,38400,"8N1","RTSRTS")
pCaptureOn("sendxcapture.html",0)


filetosendx="c:\winnt\coffee bean.bmp"


; Set serial timeouts to 10 seconds
pTimeOut(port,10000)

; Wait for a call
BoxText("Waiting for Call")
pModemAnsCall(port,1,1)


; Send "Hello".  Make sure we get s "2U2" response.  Try until it works
BoxText("Get processes in sync here")
flag=0
while flag == 0
   pPutString(port,"Hello")
   flag=pWaitFor(port,"2U2"," ",0, 3000)
endwhile




;Send file 
pSendFile(port,"ZMODEM",filetosendx,0,1)

; Wait for ByeBye message from remote computer, then hangup
BoxText("Waiting for ByeBye prompt")
pWaitFor(port,"ByeBye"," ",0,10000)



BoxText("Hanging up")
pModemHangup(port)
BoxText("Closing Port")
pComClose(port,0)
Message("All","Done")

B-SerialModemDialRecvfile.wbt

;Modem Dial Recv File
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Setup program, load extenders, turn on debugging, open com port
DebugTrace(@on,"recvxtrace.txt")
BoxOpen("Modem recvx File","")
AddExtender("wwser34I.dll")
ComPort="COM2"
BoxText("Opening Port")
port=pComOpen(ComPort,0,38400,"8N1","RTSRTS")
pCaptureOn("recvxcapture.html",0)

;Define filename to recvx
TargetDir="C:\Temp\xxx.txt"


; Set serial timeouts to 10 seconds
pTimeOut(port,10000)

; Dial and connect to remote computer
BoxText("Dialing")
pModemDial(port,1,"9,,9355518")
BoxText("Connecting")
pModemConnect(port)



;Wait for a "Hello" message.  Keep trying until we get a "Hello"
BoxText("Get processes in sync here")
flag=0
while flag == 0
   flag = pWaitfor(port,"Hello"," ",0,3000)
endwhile

;We received a "Hello".  Send a "2U2" to confirm receipt.
pPutLine(port,"2U2")


; Now recvx the file itself
precvFile(port,"ZMODEM",TargetDir,1,1)


;Send byebye message to tell other script to hangup
BoxText("Print byebye message")
pPutLine(port,"ByeBye")


BoxText("Hanging up")
pModemHangup(port)
BoxText("Closing port")
pComClose(port,0)
Message("All","Done")

And to view the debugtrace files and log of the file transfer, see:

sendxcapture.html

recvxcapture.html


Article ID:   W14555
Filename:   File Transfer Sample Code.txt
File Created: 2017:08:29:11:33:20
Last Updated: 2017:08:29:11:33:20