Powershell delete old files from remote FTP
How would I delete files that are matched with this criteria?
Thanks for your help.
### DelLogFilesWinSCP Script###
### Used to remove files that are older than 60 Days from Smartfile FTP Service ###
### Uses .Net Assembly from WinSCP ###
### Written by #### on 12/10/2013 ###
#Used to call .dll Assembly and Executable
$winscpPath = "C:\Program Files (x86)\WinSCP"
try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("$winscpPath\WinSCP.dll") | Out-Null
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "xxxxx"
$sessionOptions.UserName = "xxxxx"
$sessionOptions.Password = "xxxxx"
$session = New-Object WinSCP.Session
$session.ExecutablePath = "$winscpPath\WinSCP.exe"
try
{
$filedate = (Get-Date).Adddays(-80)
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directory = $session.ListDirectory("/")
foreach ($fileInfo in $directory.Files)
{
if ($fileInfo.LastWriteTime -lt $filedate)
{
Write-Host ("{0} with size {1}, permissions {2} and last modification at {3}" -f
$fileInfo.Name, $fileInfo.Length, $fileInfo.FilePermissions, $fileInfo.LastWriteTime)
$fileInfo.Name -delete
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
Thanks for your help.
### DelLogFilesWinSCP Script###
### Used to remove files that are older than 60 Days from Smartfile FTP Service ###
### Uses .Net Assembly from WinSCP ###
### Written by #### on 12/10/2013 ###
#Used to call .dll Assembly and Executable
$winscpPath = "C:\Program Files (x86)\WinSCP"
try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("$winscpPath\WinSCP.dll") | Out-Null
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "xxxxx"
$sessionOptions.UserName = "xxxxx"
$sessionOptions.Password = "xxxxx"
$session = New-Object WinSCP.Session
$session.ExecutablePath = "$winscpPath\WinSCP.exe"
try
{
$filedate = (Get-Date).Adddays(-80)
# Connect
$session.Open($sessionOptions)
# Get list of files in the directory
$directory = $session.ListDirectory("/")
foreach ($fileInfo in $directory.Files)
{
if ($fileInfo.LastWriteTime -lt $filedate)
{
Write-Host ("{0} with size {1}, permissions {2} and last modification at {3}" -f
$fileInfo.Name, $fileInfo.Length, $fileInfo.FilePermissions, $fileInfo.LastWriteTime)
$fileInfo.Name -delete
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}