Delete all files older than two days, except the ones beginning with a specific string

Advertisement

jgolinuc
Guest

Delete all files older than two days, except the ones beginning with a specific string

Hi,

I'm trying to delete all files older than two days, except the one beginning with "database*".

I was going with
rm *<2D|database*
, but no sucess, so I tried
rm *|database*<2D
but still not working.
I'm trying also with
ls
instead of
rm
to display the results, and as soon as I use the exclude pipe
|
I get a no result match.

How should I do this ?

I'm running the script through a .bat file with the following code

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Scripts\logs\%~1.log" /ini=nul ^
  /command ^
    "open ftp://user:pass@exemple.com/ -rawsettings ProxyPort=0" ^
    "cd /default/%~1/" ^
    "rm database*<30D" ^
    "rm *|database*<2D" ^
    "rm *<1K" ^
    "exit"

The goal is to delete all database files older than 30 days, then delete everything else older than 2 days and finally delete all files smaller than 1KB.

Thanks for the help.

Regards,

Joel

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,587
Location:
Prague, Czechia

Re: Delete all files older than two days, except the ones beginning with a specific string

It's not possible with WinSCP scripting.
But you do it with WinSCP .NET assembly from a PowerShell script.
https://winscp.net/eng/docs/library_powershell

Here is an example that does almost what you need:
https://stackoverflow.com/q/38616995/850848#38617996
You just need to add a file name test, like:
$oldFiles =
    $directoryInfo.Files |
    Where-Object { -Not $_.IsDirectory } | 
    Where-Object { $_.LastWriteTime -lt $limit } |
    Where-Object { $_.Name -notlike "database*" }
(untested)

Reply with quote

Advertisement

You can post new topics in this forum