FTP upload i download przez vbscript

FTP upload i download przez vbscript

Poniżej prezen­tu­je­my skrypt VBscript który umożli­wia pobieranie i wysyłanie plików na zdal­ny ser­w­er za pomocą pro­tokołu ftp. Skrypt posi­a­da dwie funkc­je — upload i download… 
Pozostaje tylko wywołać skrypt — dla down­loadu skład­nia będzie miała postać:

Wscript.Echo FTPDownload("192.168.1.1", "domain\user", "pass­word", "C:\", "\", "*")

Poniżej cały skrypt posi­ada­ją­cy 2 funkc­je — FTPUpload i FTPDownload:

—————————————————————-

Function FTPUpload(sSite, sUsername, sPassword, sLocalFile, sRemotePath)
'This script is pro­vid­ed under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for com­mer­cial pur­pos­es with out the expressed writ­ten consent
'of NateRice.com

Const OpenAsDefault = ‑2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

sRemotePath = Trim(sRemotePath)
sLocalFile = Trim(sLocalFile)

'———-Path Checks———
'Here we willcheck the path, if it contains
'spaces then we need to add quotes to ensure
'it pars­es correctly.
If InStr(sRemotePath, " ") > 0 Then
If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
sRemotePath = """" & sRemotePath & """"
End If
End If

If InStr(sLocalFile, " ") > 0 Then
If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then
sLocalFile = """" & sLocalFile & """"
End If
End If

'Check to ensure that a remote path was
'passed. If it's blank then pass a "\"
If Len(sRemotePath) = 0 Then
'Please note that no premp­tive check­ing of the
'remote path is done. If it does not exist for some
'rea­son. Unexpected results may occur.
sRemotePath = "\"
End If

'Check the local path and file to ensure
'that either the a file that exists was
'passed or a wild­card was passed.
If InStr(sLocalFile, "*") Then
If InStr(sLocalFile, " ") Then
FTPUpload = "Error: Wildcard uploads do not work if the path con­tains a " & _
"space." & vbCRLF
FTPUpload = FTPUpload & "This is a lim­i­ta­tion of the Microsoft FTP client."
Exit Function
End If
ElseIf Len(sLocalFile) = 0 Or Not oFTPScriptFSO.FileExists(sLocalFile) Then
'noth­ing to upload
FTPUpload = "Error: File Not Found."
Exit Function
End If
'——–END Path Checks———

'build input file for ftp command
sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF
sFTPScript = sFTPScript & sPassword & vbCRLF
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF
sFTPScript = sFTPScript & "bina­ry" & vbCRLF
sFTPScript = sFTPScript & "prompt n" & vbCRLF
sFTPScript = sFTPScript & "put " & sLocalFile & vbCRLF
sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

'Write the input file for the ftp command
'to a tem­po­rary file.
Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
fFTPScript.WriteLine(sFTPScript)
fFTPScript.Close
Set fFTPScript = Nothing

oFTPScriptShell.Run "%com­spec% /c FTP ‑n ‑s:" & sFTPTempFile & " " & sSite & _
" > " & sFTPResults, 0, TRUE

Wscript.Sleep 1000

'Check results of transfer.
Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _
FailIfNotExist, OpenAsDefault)
sResults = fFTPResults.ReadAll
fFTPResults.Close

oFTPScriptFSO.DeleteFile(sFTPTempFile)
oFTPScriptFSO.DeleteFile (sFTPResults)

If InStr(sResults, "226 Transfer com­plete.") > 0 Then
FTPUpload = True
ElseIf InStr(sResults, "File not found") > 0 Then
FTPUpload = "Error: File Not Found"
ElseIf InStr(sResults, "can­not log in.") > 0 Then
FTPUpload = "Error: Login Failed."
Else
FTPUpload = "Error: Unknown."
End If

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function

Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, sRemotePath, _
sRemoteFile)
'This script is pro­vid­ed under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for com­mer­cial pur­pos­es with out the expressed writ­ten consent
'of NateRice.com

Const OpenAsDefault = ‑2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject")
Set oFTPScriptShell = CreateObject("WScript.Shell")

sRemotePath = Trim(sRemotePath)
sLocalPath = Trim(sLocalPath)

'———-Path Checks———
'Here we will check the remote path, if it contains
'spaces then we need to add quotes to ensure
'it pars­es correctly.
If InStr(sRemotePath, " ") > 0 Then
If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then
sRemotePath = """" & sRemotePath & """"
End If
End If

'Check to ensure that a remote path was
'passed. If it's blank then pass a "\"
If Len(sRemotePath) = 0 Then
'Please note that no premp­tive check­ing of the
'remote path is done. If it does not exist for some
'rea­son. Unexpected results may occur.
sRemotePath = "\"
End If

'If the local path was blank. Pass the current
'work­ing direcory.
If Len(sLocalPath) = 0 Then
sLocalpath = oFTPScriptShell.CurrentDirectory
End If

If Not oFTPScriptFSO.FolderExists(sLocalPath) Then
'des­ti­na­tion not found
FTPDownload = "Error: Local Folder Not Found."
Exit Function
End If

sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory
oFTPScriptShell.CurrentDirectory = sLocalPath
'——–END Path Checks———

'build input file for ftp command
sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF
sFTPScript = sFTPScript & sPassword & vbCRLF
sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF
sFTPScript = sFTPScript & "bina­ry" & vbCRLF
sFTPScript = sFTPScript & "prompt n" & vbCRLF
sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCRLF
sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF
sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%")
sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName
sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName

'Write the input file for the ftp command
'to a tem­po­rary file.
Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True)
fFTPScript.WriteLine(sFTPScript)
fFTPScript.Close
Set fFTPScript = Nothing

oFTPScriptShell.Run "%com­spec% /c FTP ‑n ‑s:" & sFTPTempFile & " " & sSite & _
" > " & sFTPResults, 0, TRUE

Wscript.Sleep 1000

'Check results of transfer.
Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _
FailIfNotExist, OpenAsDefault)
sResults = fFTPResults.ReadAll
fFTPResults.Close

'oFTPScriptFSO.DeleteFile(sFTPTempFile)
'oFTPScriptFSO.DeleteFile (sFTPResults)

If InStr(sResults, "226 Transfer com­plete.") > 0 Then
FTPDownload = True
ElseIf InStr(sResults, "File not found") > 0 Then
FTPDownload = "Error: File Not Found"
ElseIf InStr(sResults, "can­not log in.") > 0 Then
FTPDownload = "Error: Login Failed."
Else
FTPDownload = "Error: Unknown."
End If

Set oFTPScriptFSO = Nothing
Set oFTPScriptShell = Nothing
End Function

 

—————————————————————-

Tags: , , , ,

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Zadzwoń teraz!