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 ...
随机推荐
- Redis学习笔记之入门基础知识——简介
非关系型数据库,存储的数据类型:字符串(STRING).列表(LIST).集合(SET).散列表(HASH).有序集合(ZSET) 持久化:时间点转储(point-in-time-dump)(快照). ...
- 为什么HashMap不是线程安全的
电面突然被问到这个问题,之前看到过,但是印象不深,导致自己没有答出来,现在总结一下. HashMap的内部存储结构 transient Node<K,V>[] table; static ...
- String基础
一: String,StringBuffer与StringBuilder的区别??String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程 ...
- javascript 函数的几种声明函数以及应用环境
本页只列出常用的几种方式,当然还有比如new Function()以及下面三种的组合. 1.函数式声明 例子:function sum(a,b){ return a+b; }; 2.函数表达式声明(匿 ...
- Java单元测试框架 JUnit
Java单元测试框架 JUnit JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于KentBeck的sUnit的xUnit家族中为最成功的一 ...
- ThinkPHP框架知识(比较全的知识)
php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格 ...
- 设置matplotlib画图支持中文显示
1.安装中文字体 git clone https://github.com/tracyone/program_font && cd program_font && ./ ...
- SQL Server Collation解惑
某些产品会有固定的DB Collation,如果提前创建DB的时候没有按照要求指定对应的Collation,这个时候就会报错,提示你Collation不匹配.在安装SQL Server的时候有时候需要 ...
- node之文件的下载
/** * 文件的下载 */ let express = require('express'); let app = express(); app.get('/',(req,res)=>{ re ...
- vsphere web client 使用中文的解决办法
1. 很多网站这么说的: vsphere web client的默认URL为:https://hostname:9443/vsphere-client 可以在URL后面加上一个参数来指定区域语言 英语 ...