Guest User

Untitled

a guest
Feb 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. $ftpServer = "RandomFTPServer"
  2. $ftpUser = "Username"
  3. $ftpPassword = Read-Host "Password" -AsSecureString
  4.  
  5. $credentials = New-ObjectSystem.Net.NetworkCredential($ftpUser, $ftpPassword)
  6.  
  7. function Get-FtpRequest($ftpPath) {
  8. $ftpRequest = [System.Net.FtpWebRequest]::Create("$ftpServer/$ftpPath")
  9. $ftpRequest.Credentials = $credentials
  10. $ftpRequest.UseBinary = $true
  11. $ftpRequest.KeepAlive = $true
  12. $ftpRequest.UsePassive = $true
  13. return $ftpRequest
  14. }
  15.  
  16. # Load WinSCP .NET assembly
  17. Add-Type -Path "WinSCPnet.dll"
  18.  
  19. # Setup session options
  20. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  21. Protocol = [WinSCP.Protocol]::Ftp
  22. HostName = "ftp.example.com"
  23. UserName = "username"
  24. Password = "password"
  25. }
  26.  
  27. try
  28. {
  29. # Connect
  30. $session = New-Object WinSCP.Session
  31. $session.Open($sessionOptions)
  32.  
  33. # List files
  34. $remotePath = "/remote/path"
  35. $directoryInfo = $session.ListDirectory($remotePath)
  36.  
  37. # Find old files
  38. $limit = (Get-Date).AddDays(-15)
  39.  
  40. $oldFiles =
  41. $directoryInfo.Files |
  42. Where-Object { -Not $_.IsDirectory } |
  43. Where-Object { $_.LastWriteTime -lt $limit }
  44.  
  45. # Delete them
  46. foreach ($oldFileInfo in $oldFiles)
  47. {
  48. $oldFilePath =
  49. [WinSCP.RemotePath]::EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)
  50. $session.RemoveFiles($oldFilePath).Check()
  51. }
  52.  
  53. Write-Host "Done"
  54. }
  55. finally
  56. {
  57. # Disconnect, clean up
  58. $session.Dispose()
  59. }
  60.  
  61. : delete files older than 7 days from ftp://my.ftpsite.net/folder/subfolder
  62. ftpuse F: my.ftpsite.net password /USER:username
  63. timeout /t 5
  64. forfiles -p "F:foldersubfolder" -s -m *.* -d -7 -c "cmd /C DEL @File /Q"
  65. ftpuse F: /DELETE
Add Comment
Please, Sign In to add comment