Sunday, March 4, 2012

File Upload and Download Automation to SFTP

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

 If (oFTPScriptFSO.FileExists("C:\Users\bdhinakaran\Desktop\sftpfile.txt")) Then
  ' do nothing
 Else
      Set writeObject= oFTPScriptFSO.CreateTextFile("C:\Users\bdhinakaran\Desktop\sftpfile.txt", True)
      writeObject.WriteLine("to test sftp")
     
 End If

 sUpldCmd = """C:\Program Files (x86)\WinSCP\WinSCP.com"" -script=" & "C:\Users\bdhinakaran\Desktop\put.txt"
 sDwnldCmd = """C:\Program Files (x86)\WinSCP\WinSCP.com"" -script=" & "C:\Users\bdhinakaran\Desktop\get.txt"
 'WScript.Echo "run Command: " & sCmd
 sReturn = oFTPScriptShell.Run(sUpldCmd,3,true)

 'WScript.Echo "Run Return Value: " & sreturn

 Wscript.Sleep 1000

 'Delete the file once it is uploaded.
 If ( sReturn = 0) Then
 If (oFTPScriptFSO.FileExists("C:\Users\bdhinakaran\Desktop\sftpfile.txt")) Then
 oFTPScriptFSO.DeleteFile("C:\sftpfile.txt")
 Else
 WScript.Echo "unable to delete the Uploaded File|0"
 WScript.Quit (2)
 End If
 Else
 Wscript.Echo "unable to login to SFTP Server"
 Wscript.Quit (2)
 End If
 sReturn = 0
 'procedures to download the file and delete after that
 sReturn = oFTPScriptShell.Run(sDwnldCmd,3,true)
 WScript.Sleep 1000

 'WScript.Echo "Run Return Value: " & sreturn
 If ( sReturn = 0) Then
 If (oFTPScriptFSO.FileExists("C:\Users\bdhinakaran\Desktop\sftpfile.txt")) Then
 WScript.Echo "SFTP is doing good | 1"
 WScript.Quit (0)
 Else
 WScript.Echo "File sftpfile.txt has not downloaded, check manually|0"
 WScript.Quit (2)
 End If
 Else
 Wscript.Echo "unable to login to SFTP Server"
 End If

 Set oFTPScriptShell = Nothing
 Set oFTPScriptFSO = Nothing

No comments:

Post a Comment