Search code examples
batch-filesftpwinscp

Is it possible to recursively delete files with more than X days in WinSCP batch file?


I have a script to transfer files from one server to other one. With this I need to have a part of script to delete files with more than X days on the destination folder.

I check the WinSCP documentation, forum and all other information and I didn't find out anything, anyone can help me.

I've tried loops to get inside folders and delete each file with more than X days, nothing worked for me.


Solution

  • No, it is not possible with simple WinSCP scripting.

    But it is easy with WinSCP .NET assembly and e.g. a PowerShell script.

    At the end of my answer to this question, you will find a full code for non-recursive delete:
    Delete files older than X days from FTP server with PowerShell or batch file

    To find old files recursively, modify it with use of Session.EnumerateRemoteFiles like this:

    $oldFiles  =
        $session.EnumerateRemoteFiles(
            "/remote/path", $Null, [WinSCP.EnumerationOptions]::AllDirectories) |
        Where-Object { -Not $_.IsDirectory } | 
        Where-Object { $_.LastWriteTime -lt $limit }