简体   繁体   中英

Delete files older than X days from FTP server with PowerShell or batch file

I have to write a script which accesses an FTP server, and then deletes all *.zip files which are older than X days.

As clarification: The script can't run on the FTP server.

This is what I have so far:

$ftpServer = "RandomFTPServer"
$ftpUser = "Username"
$ftpPassword = Read-Host "Password" -AsSecureString

$credentials = New-ObjectSystem.Net.NetworkCredential($ftpUser, $ftpPassword)

function Get-FtpRequest($ftpPath) {
    $ftpRequest = [System.Net.FtpWebRequest]::Create("$ftpServer/$ftpPath")
    $ftpRequest.Credentials = $credentials
    $ftpRequest.UseBinary = $true 
    $ftpRequest.KeepAlive = $true
    $ftpRequest.UsePassive = $true
    return $ftpRequest
}

Any tips on what I need to do next?

You have to retrieve timestamps of remote files to select the old ones.

Unfortunately, there's no really reliable and efficient way to retrieve timestamps using features offered by .NET framework/PowerShell as it does not support FTP MLSD command.

So either you use:

  • ListDirectoryDetails method (FTP LIST command) to retrieve details of all files in a directory and then you deal with FTP server specific format of the details (*nix format similar to ls *nix command is the most common, drawback is that the format may change over time, as for newer files "May 8 17:48" format is used and for older files "Oct 18 2009" format is used)
  • GetDateTimestamp method (FTP MDTM command) to individually retrieve timestamps for each file. Advantage is that the response is standardized by RFC 3659 to YYYYMMDDHHMMSS[.sss] . Disadvantage is that you have to send a separate request for each file, what can be quite inefficient.

Some references:

Though Microsoft does not recommend FtpWebRequest for a new development anyway.


Alternatively you can use a 3rd party FTP client implementation that supports the modern MLSD command and/or has built-in support for parsing different formats of the LIST command.

For example, WinSCP .NET assembly supports both.

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "username"
    Password = "password"
}

try
{
    # Connect
    $session = New-Object WinSCP.Session
    $session.Open($sessionOptions)

    # List files
    $remotePath = "/remote/path"
    $directoryInfo = $session.ListDirectory($remotePath)

    # Find old files
    $limit = (Get-Date).AddDays(-15)

    $oldFiles =
        $directoryInfo.Files |
        Where-Object { -Not $_.IsDirectory } | 
        Where-Object { $_.LastWriteTime -lt $limit }

    # Delete them
    foreach ($oldFileInfo in $oldFiles)
    {
        $session.RemoveFile($oldFileInfo.FullName).Check()
    } 

    Write-Host "Done"
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}

If you can do with a plain batch file, it's actually even easier with WinSCP scripting :

winscp.com /ini=nul /log=delete.log /command ^
    "open ftp://username:password@ftp.example.com/" ^
    "rm /remote/path/*<15D" ^
    "exit"

See file masks with time constraints .

(I'm the author of WinSCP)


Note that WinSCP does not require any installation. So you can just have its binaries copied around with your batch file or PowerShell script.

I'm currently doing this using FTPUSE , a freeware command-line tool which maps a FTP folder to a windows drive letter, together with a batch file in the following way:

: delete files older than 7 days from ftp://my.ftpsite.net/folder/subfolder
ftpuse F: my.ftpsite.net password /USER:username
timeout /t 5
forfiles -p "F:\folder\subfolder" -s -m *.* -d -7 -c "cmd /C DEL @File /Q"
ftpuse F: /DELETE

The software is compatible with all major versions of windows: Windows XP, Vista, 7, Server 2003, Server 2008, Windows 8, Server 2012 and Windows 10 (32-bit, 64-bit).

For further info, you can also read this post I wrote about FTPUSE (I'm not the author in any way, I just find it very useful for these kind of tasks).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM