Topic "Powershell delete old files from remote FTP"

Author Message
garrettpk@yahoo.com
[View user's profile]

Joined: 2013-12-10
Posts: 3
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
}
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
It looks to me that you have all the code in place already. You just need to call Session.RemoveFiles:
http://winscp.net/eng/docs/library_session_removefiles
_________________
Martin Prikryl
garrettpk@yahoo.com
[View user's profile]

Joined: 2013-12-10
Posts: 3
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.
garrettpk@yahoo.com
[View user's profile]

Joined: 2013-12-10
Posts: 3
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)
}
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
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.
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License