Remove all files in all Sub Folders named *.trn older than 7 days

Advertisement

traveller198
Guest

Remove all files in all Sub Folders named *.trn older than 7 days

Hi Everyone,

I have the following batch script I am using to try and delete all .trn files from all subfolders of a specific directory:
@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="PATH\winscp.log" /logsize=5*100M /ini=nul ^
  /command ^
    "open sftp://USER:PASS@SERVER/ -hostkey=""HOSTKEY""" ^
    "cd /PATH TO TOP LEVEL DIRECTORY/" ^
    "rm *.trn<7D" ^
    "close" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%
When I run the script nothing is deleted and the logs show:
Script: No file matching '*.trn<7D' found.
There are no .trn files in the root folder the script CD's in to, but there are many older than 7 days, 1 or more sub folders below. How can I get the script looking in all sub folders?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,378
Location:
Prague, Czechia

Re: Remove all files in all Sub Folders named *.trn older than 7 days

This is not easy. You better use WinSCP .NET assembly from e.g. a PowerShell script.
Use Session.EnumerateRemoteFiles to find the files you need and then call Session.RemoveFile for each identified file.
https://winscp.net/eng/docs/library_session_enumerateremotefiles
https://winscp.net/eng/docs/library_session_removefile

Reply with quote

Advertisement

You can post new topics in this forum