Powershell delete old files from remote FTP

Advertisement

garrettpk@yahoo.com
Joined:
Posts:
3

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
}

Reply with quote

Advertisement

garrettpk@yahoo.com

If you look at the code I have so far you will notice that I am trying to only remove files that are older than a certain date. I guess I am unclear on how I would use the $session.removefiles in the code I already have. Would I need to define a new session object that only matches my criteria like I did for the directory listings? If so could somebody point me in the right direction I would be grateful.

Reply with quote

garrettpk@yahoo.com
Joined:
Posts:
3

Problem Solved

Here is my solution. Might not be the prettiest thing but seems to work.

Had to turn on debugging to discover the .. issue. Maybe the result should be removed from the directory listing method?

try
{
# Removes files older than this date
$RemovalDate = (Get-Date).Adddays(-80)

# Connect
$session.Open($sessionOptions)

# Get list of files in the directory
$Directory = $session.ListDirectory("/")

foreach ($FileInfo in $Directory.Files)
{
###### Compare LastWriteTime and removes directory listing for .. since it bombed script
if ($FileInfo.LastWriteTime -lt $RemovalDate -and $FileInfo.Name -ne "..")
{
$RemovalFile = ($FileInfo.Name)
#write-host $RemovalFile
$session.RemoveFiles($RemovalFile)
}
}

Reply with quote

martin
Site Admin
martin avatar

Re: Problem Solved

garrettpk@yahoo.com wrote:

Here is my solution. Might not be the prettiest thing but seems to work.
Good, that's exactly, what I've meant.

Reply with quote

Advertisement

You can post new topics in this forum