Post a reply :: Support Forum

Tip: Styles can be applied quickly to selected text.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Deletion of files within folders in root folder

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

$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!
daanbrg

Deletion of files within folders in root folder

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:

"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