Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ftpServer = "RandomFTPServer"
- $ftpUser = "Username"
- $ftpPassword = Read-Host "Password" -AsSecureString
- $credentials = New-ObjectSystem.Net.NetworkCredential($ftpUser, $ftpPassword)
- function Get-FtpRequest($ftpPath) {
- $ftpRequest = [System.Net.FtpWebRequest]::Create("$ftpServer/$ftpPath")
- $ftpRequest.Credentials = $credentials
- $ftpRequest.UseBinary = $true
- $ftpRequest.KeepAlive = $true
- $ftpRequest.UsePassive = $true
- return $ftpRequest
- }
- # Load WinSCP .NET assembly
- Add-Type -Path "WinSCPnet.dll"
- # Setup session options
- $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
- Protocol = [WinSCP.Protocol]::Ftp
- HostName = "ftp.example.com"
- UserName = "username"
- Password = "password"
- }
- try
- {
- # Connect
- $session = New-Object WinSCP.Session
- $session.Open($sessionOptions)
- # List files
- $remotePath = "/remote/path"
- $directoryInfo = $session.ListDirectory($remotePath)
- # Find old files
- $limit = (Get-Date).AddDays(-15)
- $oldFiles =
- $directoryInfo.Files |
- Where-Object { -Not $_.IsDirectory } |
- Where-Object { $_.LastWriteTime -lt $limit }
- # Delete them
- foreach ($oldFileInfo in $oldFiles)
- {
- $oldFilePath =
- [WinSCP.RemotePath]::EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)
- $session.RemoveFiles($oldFilePath).Check()
- }
- Write-Host "Done"
- }
- finally
- {
- # Disconnect, clean up
- $session.Dispose()
- }
- : delete files older than 7 days from ftp://my.ftpsite.net/folder/subfolder
- ftpuse F: my.ftpsite.net password /USER:username
- timeout /t 5
- forfiles -p "F:foldersubfolder" -s -m *.* -d -7 -c "cmd /C DEL @File /Q"
- ftpuse F: /DELETE
Add Comment
Please, Sign In to add comment