Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. $yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
  2. # OR I have to use ToString("MM/dd/yyyy") for months 10-12, but I need both formats to work.
  3. #delete the temporary file
  4. del .FTPfiles.txt
  5. # Load WinSCP .NET assembly
  6. Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
  7.  
  8. # Setup session options
  9. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  10. Protocol = [WinSCP.Protocol]::Ftp
  11. HostName = "server.com"
  12. UserName = "joe"
  13. Password = "smith"
  14. }
  15.  
  16. $session = New-Object WinSCP.Session
  17.  
  18. try
  19. {
  20. # Connect
  21. $session.Open($sessionOptions)
  22.  
  23. $directory = $session.ListDirectory("/Folder")
  24.  
  25. foreach ($fileInfo in $directory.Files)
  26. {
  27. Write-Output ("{1} {0}" -f
  28. $fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
  29. }
  30. $fileList = get-content .FTPfiles.txt | findstr $yesterday
  31. $stripped = $fileList -creplace '^.*Z12', 'Z12'
  32.  
  33.  
  34. # Download files
  35. $remotePath = "/Folder/"
  36. $transferOptions = New-Object WinSCP.TransferOptions
  37. $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
  38.  
  39. $lines = $stripped
  40.  
  41. foreach ($line in $lines)
  42. {
  43. Write-Host ("Downloading {0} ..." -f $line)
  44. $session.GetFiles($remotePath+$line, "C:Downloads").Check()
  45. }
  46.  
  47. }
  48.  
  49.  
  50. catch [Exception]
  51. {
  52. Write-Host ("Error: {0}" -f $_.Exception.Message)
  53. exit 1
  54. }
  55. finally
  56. {
  57. # Disconnect, clean up
  58. $session.Dispose()
  59. }
  60.  
  61. FROM:
  62. 3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
  63. 3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
  64. 3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
  65. 3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
  66.  
  67. TO:
  68. Z1234_20170314050001_1.zip
  69. Z1234_20170315060002_1.zip
  70. Z1234_20170316070001_1.zip
  71. Z1234_20170317080001_1.zip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement