Advertisement
Guest User

Untitled

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