SCADA DIAView VBS 实现 FTP 上传与下载( 二 )


'fileExt=filePathArray(UBound(filePathArray))
'strFtpUrl=strFtpUrl&"/"&strNewFileName&"."&fileExt
'暗示上传全数
If strFileName="*" then
Set FD = objShell.NameSpace(strFtpUrl)
FD.CopyHere ofitem,4096 '若是存在并笼盖
If
If strFileName<>"*" And strFileName = ofitem.Name Then
'strFtpUrl=strFtpUrl&strNewFileName&
Set FD = objShell.NameSpace(strFtpUrl)
FD.CopyHere ofitem,4096 '若是存在并笼盖
If

Next

MsgBox "上传当作功!"

5按钮“下载”==>事务“左键按下”
'FTP IP地址如:192.168.1.65 或 ftp.wwww.baidu.com
str_server=Trim(txtftpip.Text)
'FTP 登录用户名
str_user=Trim(txtftpuser.Text)
'FTP 登录用户暗码
str_password=Trim(txtftppwd.Text)
'被上传的文件目次
str_localDir =Trim(txtlocalpath0.Text)
'被上传的文件名称 多个用英文逗号离隔;*好暗示全数文件
str_remoteFiles =Trim(txtlocalfile0.Text)
'FTP下载目次
str_remoteDir="\"&Trim(txtftpfolder0.Text)

ftp_configFile = "listfiles.ini"
Set ws = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")

'处置路径中的空格
str_remoteDir = Trim(str_remoteDir)
If InStr(str_remoteDir," ")>0 Then
If Left(str_remoteDir,1)<> """" And Right(str_remoteDir,1) <> """" Then
str_remoteDir = """" &str_remoteDir& """"
If
If

'设置工作路径
originalWorkingDirectory = ws.CurrentDirectory
ws.CurrentDirectory = str_localDir

'生当作ftp号令
ftpScript_str = ""
ftpScript_str = ftpScript_str & "USER " & str_user & vbCrLf
ftpScript_str = ftpScript_str & str_password & vbCrLf
ftpScript_str = ftpScript_str & "cd " & str_remoteDir & vbCrLf
ftpScript_str = ftpScript_str & "binary" & vbCrLf
ftpScript_str = ftpScript_str & "prompt off" & vbCrLf

remoteFiles_obj = Split(str_remoteFiles,",")
For Each remoteFile_str In remoteFiles_obj
'remoteFile_str = Left(remoteFile_str,Len(remoteFile_str)-1) '去除回车符(为什么会有换行符?)
If InStr(remoteFile_str, " ")>0 Then
remoteFile_str = """" & remoteFile_str & """"
If
ftpScript_str = ftpScript_str & "get " & remoteFile_str & vbCrLf
Next

ftpScript_str = ftpScript_str & "bye" & vbCrLf & "quit" & vbCrLf & "quit" & vbCrLf

'建立姑且文件
tempDir_str = ws.ExpandEnvironmentStrings("%TEMP%")
scriptFilePath_str = tempDir_str& "\" &fs.GetTempName
ftpResultPath_str = tempDir_str& "\" &fs.GetTempName

'姑且ftp剧本文件
Set scriptFile_obj = fs.OpenTextFile(scriptFilePath_str,2,True)
scriptFile_obj.Write(ftpScript_str)
scriptFile_obj.Close

'执行ftp剧本
'logFileObject.WriteLine Time & " Downloading files from ftp..."
ws.Run "%comspec% /c FTP -n -s:" &""""&scriptFilePath_str&""""& " " &str_server& " > " &ftpResultPath_str,0,True
'WScript.Sleep 1000

'姑且ftp成果文件
Set resultFile_obj = fs.OpenTextFile(ftpResultPath_str,1)
successDownloadCount_int = 0
failToDownloadCount_int = 0
'WScript.Echo scriptFilePath_str & " " &ftpResultPath_str
'WScript.Echo ftpScript_str

Do Until resultFile_obj.AtOfStream
currLine_str = resultFile_obj.ReadLine
'WScript.Echo currLine_str

' If InStr(currLine_str,"get ")>0 And InStr(currLine_str,"forme")>0 Then
If InStr(currLine_str,"get ")>0 Then
currLine_str = Right(currLine_str,Len(currLine_str)-(InStr(currLine_str,"get ")+3))
nextLine_str = resultFile_obj.ReadLine
nextLine_str = resultFile_obj.ReadLine
nextLine_str = resultFile_obj.ReadLine

If InStr(nextLine_str,"226 Transfer complete")>0 Then
successDownloadCount_int = successDownloadCount_int + 1
'logFileObject.WriteLine Time & " Finish downloading file(" &currLine_str& ")"

推荐阅读