Deleting remote .csv files with timestamp in their names older than 2 days

匆匆过客 提交于 2019-12-08 05:17:17

问题


I am trying to delete remote (.csv) files in the FTP server older than 2 days files.

The files do not have their last modification time set correctly. I have to rely on a timestamp in their names.

Naming of the file is like Sales_201705010315.csv (date and time).

My current WinSCP script is:

option batch on
option confirm off
open login ftp credentials
cd /OUT
rm *<1D
exit

When I run the script, files are not deleting. Can someone please correct my scripting


回答1:


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"



回答2:


Be aware that the file on the FTP server will have the date/time the file was created on the FTP server, not the original file's date/time. So if your file was transferred by some automated task that is run overnight the FTP server date/time may be different. If the date/time on the FTP server isn't synced to a time server you'll run into the same problem. And you can experience the problem if the FTP transmitting machine and receiving machine are in different time zones.



来源:https://stackoverflow.com/questions/43746835/deleting-remote-csv-files-with-timestamp-in-their-names-older-than-2-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!