2

I have two cams at my house, creating images nearly every day. They save them to my FTP-Server (Fritz.Box\Nas drive).

The folder structure is as follows:

+-2016-08-24
+-+Subfolder
+----+AnotherSubfolder
+-------+File.jpg
+-2016-08-25
+-+Subfolder
+----+AnotherSubfolder
+-------+File.jpg
+-2016-08-26
+-+Subfolder
+----+AnotherSubfolder
+-------+File.jpg
...

Now I can login to my FTP via batch file (on Windows) like this:

echo user foo>ftpcmd.txt
echo 1234>>ftpcmd.txt
... CODE HERE ...
echo quit>>ftpcmd.txt
ftp -n -s:ftpcmd.txt foo.com

On Windows I would delete 2-day-old local files via

forfiles /d -2 ...

On FTP it won't work. So, how can I delete the folders AND SUBFOLDERS/SUBFILES older than 2 days?

I know about this question: Batch delete files on FTP older than x days

But I couldn't figure out a good solution for me. Again, I wanna have a batch file, that checks the folders (ex.: 2016-08-24) against the date - 2 days and delete them AND THEIR Subdirectories AND SUBFILES.

Hopefully that is not a spam question, because there is another one, asking nearly the same.

Greetings
Dirk

Community
  • 1
  • 1

1 Answers1

1

The ftp.exe does not support this. You would have to run the ftp.exe once to get a list of folders. Then you have to write some very fancy batch script to process the list, to select the old folders, and generate an ad hoc delete script. And then you will find out that the ftp.exe does not support recursive delete anyway.

If you want to do without any 3rd party dependency, you will have more luck with PowerShell (and the FtpWebRequest class). Though it will still be very difficult to implement (again no native support for recursive delete).


Though, it's easy with WinSCP FTP client, if the folders have modification timestamp matching their name.

You can use a batch file (.bat) like this:

winscp.com /ini=nul /log=ftp.log /command ^
    "open ftp://username:password@ftp.example.com/" ^
    "rm /remote/path/*<2D" ^
    "exit"

References:


It's more tricky, if you have to rely on file names only.

Though you can easily write a script to delete the 2-day-old folder:

You can use a batch file like this:

winscp.com /ini=nul /log=ftp.log /command ^
    "open ftp://username:password@ftp.example.com/" ^
    "rm /remote/path/%%TIMESTAMP-2D#yyyy-mm-dd%%" ^
    "exit"

If you schedule the script to be run every day, you will be effectively keeping only the folders for the last two days.

Reference: %TIMESTAMP% syntax.


WinSCP does not require to be installed. All you need to get the batches working is to extract a contents of WinSCP portable executables package along the batch file.


(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992