VBScript operation SFTP

Source: Internet
Author: User

The sample code mainly uses VBScript to implement the SFTP upload/download function.

'Return yyyymm base on current datefunction formatcurrentdate () currentdate = formatdatetime (date, 2) formatcurrentdate = right ("0000" & year (currentdate), 4) & right ("00" & month (currentdate), 2) end function 'upload and backup files in given foldersub uploadandbackupfiles (mysite, FS, OUTFILE, localfolder, baklocalfolder) dim ofolder, ofiles, filename set ofolder = FS. getfolder (localfolder) set ofiles = o Folder. files for each file in ofiles filename = file. name 'check Bak folder, if not exist, then create it if (not (mysite. localexists (baklocalfolder) Then mysite. createlocalfolder baklocalfolder end if (mysite. localexists (localfolder & filename) Then 'upload file if (not (mysite. remoteexists ("/inbound/" & filename) Then OUTFILE. writeline formatdatetime (now () & ": [upload]" & filename mysite. Upload localfolder & filename, "/inbound/" & filename end if 'backup file to Bak folder OUTFILE. writeline formatdatetime (now () & ": [Backup]" & filename mysite. localrename localfolder & filename, baklocalfolder & filename end if nextend sub 'Download files from remote sftpsub partition (mysite, OUTFILE, localfolder, remotefolder) dim strfilelist, strfilename, I, j mysite. localfolder = Loc Alfolder mysite. remotefolder = remotefolder if cbool (mysite. remoteexists (mysite. remotefolder) then if cbool (mysite. localexists (mysite. localfolder) then' to obtain the list of objects in the remote download directory, using "|" as the separator mysite. getlist "", "", "% name |" strfilelist = mysite. getresult if Len (strfilelist) <> 0 then I = 1 do while true J = instr (I, strfilelist, "||| ") if j <= 0 Then exit do end if strfilename = mid (strfilelist, I, j-I) OUTFILE. writeline formatdatetime (now () & ": [Download]" & strfilename mysite. download strfilename OUTFILE. writeline formatdatetime (now () & ": [* remove *]" & strfilename mysite. remoteremove strfilename is added to 5 because the separator "|" is 3 characters in length, and there are two characters including carriage return and line feed. I = J + 5 loop else OUTFILE. writeline "message! There is no file in remote SFTP "end if else OUTFILE. writeline" error! Local directory doesn' t existing "end if else OUTFILE. writeline" error! Remote directory doesn't existing "end if end subdim mysite, FOS, outfileon error resume nextset Fos = wscript. createobject ("scripting. fileSystemObject ") set OUTFILE = FOS. opentextfile ("\ 10.7.11.103 \ cusdec_edi \ schedule \ vbs_cusdec_log.txt", 8, true) OUTFILE. writeline "========================================== ========================================================== ============= "OUTFILE. writeline "Start Time:" & formatd Atetime (now () 'create teconnection objectset mysite = Createobject ("cuteftppro. teconnection ") 'initialize remote server host name, protocol, port, etc. mysite. host = "XX. XX. XX. xx "mysite. protocol = "SFTP" mysite. port = 22mysite. retries = 30mysite. delay = 30mysite. maxconnections = 2mysite. transfertype = "Auto" mysite. datachannel = "default" mysite. autorename = "off" 'Warning !!! Sensitive data: user name and password. mysite. login = "user" mysite. password = "PWD" mysite. socksinfo = "" mysite. proxyinfo = "" 'connect to remote Server' mysite. disconnectmysite. connectif cbool (mysite. isconnected) Then OUTFILE. writeline "connected to server:" & mysite. hostend ifif err. number> 0 then OUTFILE. writeline "error:" & err. description err. clearend ifcall uploadandbackupfiles (mysite, FOS, OUTFILE, "\ 10.7.11.103 \ cusdec_edi \ zb1 \", "\ 10.7.11.103 \ cusdec_edi \ archive \ zb1 \" & formatcurrentdate () & "\") Call uploadandbackupfiles (mysite, FOS, OUTFILE, "\ 10.7.11.103 \ cusdec_edi \ zb2 \", "\ 10.7.11.103 \ cusdec_edi \ archive \ zb2 \" & formatcurrentdate () & "\") Call downloadfilesfromsftp (mysite, OUTFILE, "\ 10.7.11.103 \ cusdec_edi \ zback ", "/outbound/") 'closeoutfile. writeline "End Time:" & formatdatetime (now () OUTFILE. writeline "========================================== ========================================================== ============ "& vbcrlf & vbcrlfoutfile. closeset Fos = nothingmysite. disconnect 'mysite. closewscript. quit

Finally, a scheduled task is created and poll and executed every 30 minutes.

schtasks /create /sc minute /mo 30 /tn "CUSDECEDI" /tr E:\CUSDEC\CUSDEC.vbs /ru System

 

VBScript operation SFTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.