VBScript操作SFTP
示例代码主要通过VBScript实现对SFTP的上传下载功能
' Return yyyyMM base on current date
Function FormatCurrentDate()
CurrentDate = Formatdatetime(Date,)
FormatCurrentDate = right("" & year(CurrentDate),) & right("" & month(CurrentDate),)
End Function ' Upload and Backup files in given folder
Sub UploadAndBackupFiles(MySite,fs,outFile,localFolder,bakLocalFolder)
Dim oFolder,oFiles, fileName
set oFolder = fs.GetFolder(localFolder)
set oFiles = oFolder.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 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
Next
End Sub ' Download files from remote SFTP
Sub DownloadFilesFromSFTP(MySite,outFile,localFolder,remoteFolder)
Dim strFileList,strFileName,i,j
MySite.LocalFolder = localFolder
MySite.RemoteFolder = remoteFolder
If CBool(MySite.RemoteExists(MySite.RemoteFolder)) Then
If CBool(MySite.LocalExists(MySite.LocalFolder)) Then
' 获取远程下载目录的文件列表,以"|||"作为分隔符
MySite.GetList "", "", "%NAME|||"
strFileList = MySite.GetResult
If Len(strFileList) <> Then
i =
Do While true
j = InStr(i, strFileList, "|||")
If j <= 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
'加5,因为分隔符"|||"的长度为3个字符,另外还有回车和换行2个字符
i = j +
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 Sub Dim MySite, fos, outFile On Error Resume Next Set fos = WScript.CreateObject("scripting.filesystemobject")
Set outFile = fos.OpenTextFile("\\10.7.11.103\cusdec_edi\Schedule\VBS_CUSDEC_Log.txt", , True) outFile.WriteLine "=========================================================================================="
outFile.WriteLine "Start Time: " & FormatDateTime(Now()) ' Create TEConnection object
Set MySite = CreateObject("CuteFTPPro.TEConnection") ' Initialize remote server host name, protocol, port, etc.
MySite.Host = "xx.xx.xx.xx"
MySite.Protocol = "SFTP"
MySite.Port =
MySite.Retries =
MySite.Delay =
MySite.MaxConnections =
MySite.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.Disconnect
MySite.Connect If Cbool(MySite.IsConnected) Then
outFile.WriteLine "Connected to server: " & MySite.Host
End If If Err.Number > Then
outFile.WriteLine "Error: " & Err.Description
Err.Clear
End If Call 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/") ' Close
outFile.WriteLine "End Time: " & FormatDateTime(Now())
outFile.WriteLine "==========================================================================================" & vbCrLf & vbCrLf
outFile.Close
Set fos = Nothing
MySite.Disconnect
'MySite.Close
WScript.Quit
最后建一计划任务,每隔30分钟轮询执行
schtasks /create /sc minute /mo 30 /tn "CUSDECEDI" /tr E:\CUSDEC\CUSDEC.vbs /ru System
VBScript操作SFTP的更多相关文章
- .net 操作sftp服务器
因为项目的需要,整理了一段C#操作sftp的方法. 依赖的第三方类库名称为:SharpSSH 1.1.1.13. 代码如下: 1: using System; 2: using System.Coll ...
- c# 使用Renci.SshNet.dll操作SFTP总结
1.操作类 /// <summary> /// SFTP操作类 /// </summary> public class SFTPHelper { #region 字段或属性 p ...
- Java操作SFTP
import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.jcr ...
- vbs 之 excel 使用VBScript 操作excel
打开excel及新建工作薄 '' 2. Method ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 2.1 CreateO ...
- 关于FTP和SFTP的操作总结
SFTP使用的三方类库是Renci.SshNet.DLL SFTP连接大部分网上使用IP地址形式的路径,而我本次使用的是网站形式的.类似sftp.XXX.com SFTP的操作也类似File文件的操作 ...
- Linux设置SFTP服务用户目录权限
我们有时会遇到这样的需求,限制一个Linux用户,让他只能在指定的目录下进行添加.修改.删除操作,并且只能使用sftp登录服务器,不能用ssh操作.这些可以通过配置sftp服务实现. 提供sftp服务 ...
- SFTP数据迁移
背景 服务器部署到aliyun上,之前sftp数据又是在系统盘上,由于现在数据量越来越大,导致系统盘无法满足现有要求,所以需要对sftp相关数据进行迁移至数据盘. 方案 方案一:原数据复制到新磁盘中, ...
- 如何使用sftp下载Linux服务器上的文件到本地
下载Linux服务器上的文件到本地 Linux服务器上的操作 sftp xxxxx@jumper.xxxx.com 使用put命令进行文件上传,put app.log 本地操作 sftp xxxxx@ ...
- Python模块学习 - Paramiko
简介 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography). 有了Paramiko以后,我们就可以在Py ...
随机推荐
- PAT甲题题解-1077. Kuchiguse (20)-找相同后缀
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 【Beta阶段】第三次Scrum Meeting!
每日任务内容: 本次会议为第三次Scrum Meeting会议~ 由于本次会议项目经理召开时间依旧较晚(晚上12点半),所以在公寓7层召开,女生参与了线上会议,召开时间大家集会10分钟. 队员 昨日完 ...
- [2017BUAA软工助教]个人得分总表(beta阶段)
一.表 学号 b团队 b团队得分 b贡献分 阅读作业 提问回顾 总分 14011100 hotcode5 228 60 6 7.5 301.5 14061213 PM="PokeMon&qu ...
- 第三次spring冲刺2
完成了对错题的收集,和做错题的功能,运用了android自带的SQLite数据库.
- 第一个sprint与第二个sprint阶段总结
总体: 在第一个sprint中,团队里的小伙伴都在积极努力的配合,基本按照流程做了一次Sprint,大家一块进行计划会议,一块估计任务工时,但是还是有一些意外的事情,这段时间大家都没什么精力放在这门上 ...
- HTML5之HTTP协议
---恢复内容开始--- 99%的人都理解错了HTTP中GET与POST的区别 2016.10.11 13:23:22来源: 51cto作者:51cto (转) GET和POST是HTTP请求 ...
- 正则的replace函数传参使用
<script> var str = "a1ba2b"; var reg = /a.b/g; str = str.replace(reg,function(a,b){ ...
- helm的安装于与简单使用
根据 csdn 博客整理学习 原始博客地址: https://blog.csdn.net/weiguang1017/article/details/78045013 1. 下载所需要的文件: 客户端文 ...
- Nginx 配置遇到问题
1 connect() to 10.2.20.170:1009 failed (13: Permission denied) while connecting to upstream 现这个错误是因为 ...
- 前端开发【第6篇:JavaScript客户端(浏览器)】
Web浏览器中的JavaScript 客户端JavaScript时间线 1.Web浏览器创建Document对象,并且开始解析web页面,解析HTML元素和它门的文本内容后添加Element对象和Te ...