This will indeed delete files "older than 1 day" (not 2 days):
rm *<1D
See file mask with time contraints.
But that syntax uses file modification time.
See also Delete files older than X days from FTP server with PowerShell or batch file.
If you need to select the files based on timestamp in their names, it's more complicated.
It's easy to delete files with a timestamp 2 days old:
rm Sales_%TIMESTAMP-2D#yyyymmdd%????.csv
This uses %TIMESTAMP%
syntax with a relative time. The syntax will make the command resolve to (as of 2017-05-04):
rm Sales_20170502????.csv
But that won't delete files 3 and more days old. That's not a problem, if you run the script regularly every day. If you want to cater for 1 or few days of outages, you can delete files with timestamp 2, 3, 4... days old like:
rm Sales_%TIMESTAMP-2D#yyyymmdd%????.csv
rm Sales_%TIMESTAMP-3D#yyyymmdd%????.csv
rm Sales_%TIMESTAMP-4D#yyyymmdd%????.csv
...
If you really want to delete all files with timestamp 2 and more days old, you have to write the script in a more powerful language.
Example in PowerShell with use of WinSCP .NET assembly:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.examle.com"
UserName = "username"
Password = "password"
}
# Connect
Write-Host "Connecting..."
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
Write-Host "Listing files..."
$remotePath = "/OUT"
$files = $session.ListDirectory($remotePath).Files
$prefix = "Sales_"
$twoDaysBack = (Get-Date).AddDays(-2)
$timestamp = $twoDaysBack.ToString("yyyyMMdd")
foreach ($file in $files)
{
if (($file.Name.Length -gt ($prefix.Length + $timestamp.Length)) -and
($file.Name.SubString(0, $prefix.Length) -eq $prefix) -and
($file.Name.SubString($prefix.Length, $timestamp.Length) -le $timestamp))
{
$path = [WinSCP.RemotePath]::EscapeFileMask($file.FullName)
$session.RemoveFiles($path).Check()
Write-Host "Deleted $($file.Name)"
}
}
Write-Host "Done"