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

FTP Functions

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

Multi-Function FTP Script

Keywords: FTP Winsock	

Note:Use this script at your own risk....
This is un-tested code written by one of our users and
posted to the WinBatch Tech Support Forum "WinBatch script exchange" conference.

Because uucp failed to properly copy some unix scripts I must change in conjunction with new software periodically on several dozen "WAN'ed" servers, and the unix-to-unix utility failed to give me the feedback I wanted, I wrote this script. It makes these assumptions:

  1. Same password for ftp access (I used root)
  2. That a file exists containing a list of the servers by ID (either name or IP Address)
  3. All references to J: in the script assume it is a mapped network drive to which you have read/write access.

It will run minimized just fine. The directory J:\FTPGET is used to create a short report or results file for each of the servers referenced by the list in 2 above.

AddExtender("WWWSK34I.DLL")
cr=Num2Char(13)
lf=Num2Char(10)
crlf=StrCat(cr, lf)
;Do the message
TheMes="This utility copies files between servers on the WAN to the directory of your choice.%crlf% "
TheMess2="Make certain the server list file J:\UNAME.TXT contains the list of  %crlf% servers you wish to interact with."
Mess=StrCat("%TheMes%","%TheMess2%") 
Message("FTPMore","%Mess%")
;What do you want to do?
  list = StrCat("Retrieve File",@tab,"Send File",@tab,"Get Directory Listing",@tab,"Delete File")
  Ajob = AskItemList("Select Desired Task", list, @tab, @sorted, @single)
  Display(2,"The item selected is", Ajob)
;Where are the server names?
  types="All Files|*.*|Text Files|*.txt|"
  fn1=AskFileName("Select File containing Server List", "J:\", types, "uname.txt", 1)
;  Message("File selected was", fn1)
;How do I get in to the servers
  pswd= AskPassword("Security Check", "Please enter servers' password:")
; branch and do the job
  if Ajob=="Retrieve Files" then gosub GetFiles
  if Ajob=="Send File" then gosub SendFiles
  if Ajob=="Get Directory Listing" then gosub DoDir
  if Ajob=="Delete File" then gosub KillFile
; come home and die
  Exit
; The party is over at this point

:SendFiles
  types2="All Files|*.*|Text Files|*.txt|"
  FiletoSend=AskFileName("Select file to upload to servers","F:\log\",types2,"test",1)
  d1=Strlen("%FiletoSend%")
  For a=1 to %d1%
  dd1=strsub("%FileToSend%",a,1)
  if dd1=="\" then d=a
  next
  tlen=d1-d
  d=d+1
  dd1=strsub("%FiletoSend%",d,tlen)
  DiD1 = AskLine("Destination Directory", "Please enter destination directory file name (no leading or trailing slashes, please) ", "")
  DiD1=StrCat("/","%DiD1%","/")
  DestDir=StrCat("%DiD1%","%dd1%")
;message("it goes","%DestDir%")
  dolist=FileOpen("%fn1%","READ")
  errormode(@notify)
:loophere
  while @TRUE             ; Loop till break do us end
        host = FileRead(dolist)
                If host == "*EOF*" 
                display(2,"EOF","Last Server Entry Found")
                Break
        endif
        
        userid="root"
        acct=""
        
        hSession=ftpOpen(host,userid,pswd,acct,3)
        gosub chkerrs
        if !IsNumber(hSession)
;               display(2,"FTP Open Error",hSession)
                break
        endif
        
        rs=ftpPut(hSession, "%FiletoSend%", "%DestDir%", "B")
        file1=StrCat("j:\FTPGET\","%host%",".txt")
        list=ftpList(hSession,"%DestDir%","%file1%")
        ftpClose(hSession)
;display(2,"ftpPut %FiletoSend% returned",rs)
:Nloop
endwhile
if host !="*EOF*" then goto loophere
fileclose(dolist)
Return

:GetFiles
FiletoGet=AskLine("Retrieve from servers","Enter FileName (full Unix pathname) to Retrieve from all servers","")
;DiD1 = AskLine("Destination Filename", "Please re-enter filename without path", "")
If !DirExist("J:\FTPGET\") Then DirMake("J:\FTPGET")
DirChange("J:\FTPGET\")
DiD1="J:\FTPGET\"

;DD1=StrCat("j:\ftpget","%DiD1%","/")
;DestDir=StrCat("%DiD1%","%dd1%")
;'message("it goes","%DestDir%")
dolist=FileOpen("%fn1%","READ")
errormode(@notify)
:loop2
while @TRUE             ; Loop till break do us end
        host = FileRead(dolist)
                If host == "*EOF*" 
                display(2,"EOF","Last Server Entry Found")
                Break
        endif
        
        userid="root"
        acct=""
        
        hSession=ftpOpen(host,userid,pswd,acct,3)
        gosub chkerrs
        if !IsNumber(hSession)
;               display(2,"FTP Open Error",hSession)
                break
        endif
        DestDir=StrCat("%DiD1%","%host%",".ftp")
        rs=ftpGet(hSession, "%FiletoGet%", "%DestDir%", "B")
        ftpClose(hSession)
:Nloop
endwhile
if host !="*EOF*" then goto loop2
fileclose(dolist)
Return


:DoDir
FiletoGet=AskLine("Retrieve from servers","Enter Directory to list from all servers","")
If !DirExist("J:\FTPGET\") Then DirMake("J:\FTPGET")
DirChange("J:\FTPGET\")
DiD1="J:\FTPGET\"
dolist=FileOpen("%fn1%","READ")
errormode(@notify)
:loop3
while @TRUE             ; Loop till break do us end
        host = FileRead(dolist)
                If host == "*EOF*" 
                display(2,"EOF","Last Server Entry Found")
                Break
        endif
        
        userid="root"
        acct=""
        
        hSession=ftpOpen(host,userid,pswd,acct,3)
        gosub chkerrs
        if !IsNumber(hSession)
;               display(2,"FTP Open Error",hSession)
                break
        endif
        DestDir=StrCat("%DiD1%","%host%dir",".txt")
        list=ftpList(hSession,"%FiletoGet%","%DestDir%")
        ftpClose(hSession)
:Nloop
endwhile
if host !="*EOF*" then goto loop3
fileclose(dolist)
Return

:chkerrs
        if hSession > 1
                er1=StrCat("%hSession%  ","on open with option ","%ajob%")
                f1=StrCat("J:\FTPGET\%host%",".err")
                errfile=FileOpen("%f1%","WRITE")
                FileWrite(errfile, "%er1%")
                FileClose(errfile)
                ftpClose(hSession)
        endif
return


:KillFile
FiletoKill=AskLine("Delete Remote File","Enter FileName (full Unix pathname) to delete from all servers","")
If !DirExist("J:\FTPGET\") Then DirMake("J:\FTPGET")
DirChange("J:\FTPGET\")
DiD1="J:\FTPGET\"
dolist=FileOpen("%fn1%","READ")
errormode(@notify)
:loop4
while @TRUE             ; Loop till break do us end
        host = FileRead(dolist)
                If host == "*EOF*" 
                display(2,"EOF","Last Server Entry Found")
                Break
        endif
        
        userid="root"
        acct=""
        
        hSession=ftpOpen(host,userid,pswd,acct,3)
        gosub chkerrs
        if !IsNumber(hSession)
;               display(2,"FTP Open Error",hSession)
                break
        endif
        rs=ftpDelete(hSession, "%FiletoKill%")
        ftpClose(hSession)
        if rs=="" then rs="Delete successful"
        er1=StrCat("%rs%  ","on option ","%ajob%")
        f1=StrCat("J:\FTPGET\%host%",".rpt")
        errfile=FileOpen("%f1%","WRITE")
        FileWrite(errfile, "%er1%")
        FileClose(errfile)

endwhile
if host !="*EOF*" then goto loop4
fileclose(dolist)
Return

Article ID:   W12649
Filename:   Multi-Function FTP script.txt
File Created: 2013:06:19:14:22:40
Last Updated: 2013:06:19:14:22:40