Topic "Deletion of files within folders in root folder"

Author Message
daanbrg
[View user's profile]

Joined: 2017-05-01
Posts: 1
Location: The Netherlands
Hey everyone,

I'm a volunteer tech at the local community radio station. We have a system that automatically publishes recorded shows online. It keeps them online for 4 weeks, and then removes the references to these older shows to make room for new ones.
But only the references on the webpage are removed--the audio files themselves stay on the web server.

The publishing software works in a hierarchy to upload files. Every program has a unique ID, and a new episode of the program is uploaded to a folder with the ID for that program.
So the root folder of the FTP account for this program looks like this. A bunch of folders with ID numbers for each show that the station has:

And this is a look inside one of those folders:


I'd like to automatically remove these files when they are 35 days or older. Using a WinSCP example from StackOverflow, I tried accomplishing deletion of these files with the following batch script:

Code:
"C:\Program Files (x86)\WinSCP\WinSCP.com" /ini=nul /log=C:\Users\omitted\Documents\webdelete.log /command ^
    "open ftp://omitted" ^
    "rm /*<35D" ^
    "exit"


Unfortunately, this code does not seem to look inside the various folders, but only acts on the root directory. It does delete folders and files inside the root directory that are older than 35 days, but that's not of great help because most shows have one or more new episodes every week. That would update the folder's date, and render this code useless.

I've tried searching Google and this website for clues on how to do recursive deletion, but I can't get the obvious wildcards like *.* to work properly with rm and the <35D mask. Maybe rm is the wrong tool for the job, maybe the mask doesn't work for this particular situation... but I don't know where to look any more.

If you could help me solve this puzzle, that'd be greatly appreciated.

Thanks in advance, also on behalf of the more than 90 volunteers of our station!

Cheers,
Daan Berg
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 27106
Location: Prague, Czechia
It's not possible with the rm command.

But you can use a PowerShell script with WinSCP .NET assembly.

See this:
https://stackoverflow.com/q/38616995/850848#38617996

Though it does not actually descend to subfolder either. But if you replace the Session.ListDirectory with Session.EnumerateRemoteFiles with appropriate options ([WinSCP.EnumerationOptions]::AllDirectories), it will.
https://winscp.net/eng/docs/library_session_enumerateremotefiles

Code:
$remotePath = "/"
$files = $session.EnumerateRemoteFiles($remotePath, "*", [WinSCP.EnumerationOptions]::AllDirectories)

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

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

# Delete them
foreach ($oldFileInfo in $oldFiles)
{
    $oldFilePath = $session.EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)
    $session.RemoveFiles($oldFilePath).Check()
}


Test it well, before running it on live data!
Advertisements

You can post new topics in this forum

Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License