本文介绍了删除名称超过2天的带有时间戳的远程.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除FTP服务器中早于2天的文件中的远程(.csv)文件.

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.

文件的命名类似于Sales_201705010315.csv(日期和时间).

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

我当前的WinSCP脚本是:

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天" (不是2天):

This will indeed delete files "older than 1 day" (not 2 days):

rm *<1D

请参见带有时间限制的文件掩码.

但是该语法使用文件修改时间.

But that syntax uses file modification time.

另请参见使用PowerShell或批处理文件从FTP服务器删除X天之前的文件.

如果您需要根据时间戳选择文件名,则更为复杂.

If you need to select the files based on timestamp in their names, it's more complicated.

删除带有2天时间戳记的文件很容易:

It's easy to delete files with a timestamp 2 days old:

rm Sales_%TIMESTAMP-2D#yyyymmdd%????.csv

这使用 %TIMESTAMP%带有相对时间的语法.语法将使命令解析为(从2017-05-04开始):

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

但这不会删除3天以上的文件.如果您每天定期运行脚本,那不是问题.如果您想应付1或几天的停机时间,则可以删除时间戳为2、3、4 ...天的文件,例如:

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
...


如果您确实要删除所有时间戳为2天或更多天的文件,则必须使用更强大的语言编写脚本.


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.

使用WinSCP .NET程序集的PowerShell中的示例:

# 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天的带有时间戳的远程.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:57