Deletion of files within folders in root folder

Advertisement

daanbrg
Joined:
Posts:
1
Location:
The Netherlands

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:
<invalid hyperlink removed by admin>
And this is a look inside one of those folders:
<invalid hyperlink removed by admin>

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

Reply with quote

Advertisement

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

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:
Delete files older than X days from FTP server with PowerShell or batch file

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!

Reply with quote

pladus
Joined:
Posts:
2

hello!
may plays with this command to delete files in subfolders
rm /*15D
and accidently I found this topic.

Please, tell me please if there is a option to make possible in rm (WinSCP scripting)
How can be your script above in NET, be automated in a batch?
I used these steps:
1) bat file for WinSCP
winscp /script=ftp_config.txt /log="D:\WinCSP\ftp_transfer.log"
2) file_config – open FTP and synchronize options
option batch abort
option confirm off
open ftp://MyFTPServer -passive=on -timeout=60
synchronize remote -preservetime -nopermissions -filemask="*.*" -transfer=automatic E:\BackUp\ /ftp_backup
cd /ftp_backup/
rm *<12D
exit

Reply with quote

pladus
Joined:
Posts:
2

Thanks, I will.
Before migrate to .NET, I did like on reply in topic, using FTPUSE, a freeware command-line tool which maps a FTP folder to a Windows drive letter.
Quick and not much effort.
: delete files older than 7 days from ftp://my.ftpsite.net/folder/subfolder
ftpuse F: my.ftpsite.net password /USER:username
timeout /t 5
forfiles -p "F:\folder\subfolder" -s -m *.* -d -7 -c "cmd /C DEL @File /Q"
ftpuse F: /DELETE
Maybe in next version, RD command will work to subfolders, so will be very nice.
Thanks for WinSCP!

Reply with quote

Advertisement

You can post new topics in this forum