Here is my solution. Might not be the prettiest thing but seems to work.
Good, that's exactly, what I've meant.
garrettpk@yahoo.com
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)
}
}
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.
# 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()
}