简体   繁体   English

批量 - FTP删除早于的文件夹

[英]Batch - FTP delete folders older than

I have two cams at my house, creating images nearly every day. 我家里有两个摄像头,几乎每天都会创建图像。 They save them to my FTP-Server (Fritz.Box\\Nas drive). 他们将它们保存到我的FTP服务器(Fritz.Box \\ Nas驱动器)。

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: 现在我可以通过批处理文件(在Windows上)登录我的FTP,如下所示:

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 在Windows上,我会删除2天的本地文件

forfiles /d -2 ...

On FTP it won't work. 在FTP上它将无法正常工作。 So, how can I delete the folders AND SUBFOLDERS/SUBFILES older than 2 days? 那么,如何删除超过2天的文件夹和SUBFOLDERS / SUBFILES?

I know about this question: Batch delete files on FTP older than x days 我知道这个问题: 在超过x天的FTP上批量删除文件

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. 再次,我想要一个批处理文件,检查文件夹(例如:2016-08-24)与日期 - 2天,并删除它们和他们的子目录和子目录。

Hopefully that is not a spam question, because there is another one, asking nearly the same. 希望这不是垃圾邮件问题,因为还有另一个问题,几乎一样。

Greetings 问候
Dirk 短剑

The ftp.exe does not support this. ftp.exe不支持此功能。 You would have to run the ftp.exe once to get a list of folders. 您必须运行ftp.exe一次才能获得文件夹列表。 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. 然后你会发现ftp.exe不支持递归删除。

If you want to do without any 3rd party dependency, you will have more luck with PowerShell (and the FtpWebRequest class). 如果你想没有任何第三方依赖,你将有更多的运气与PowerShell(和FtpWebRequest类)。 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. 但是,使用WinSCP FTP客户端很容易,如果文件夹的修改时间戳与其名称相匹配。

You can use a batch file ( .bat ) like this: 您可以像这样使用批处理文件( .bat ):

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: 虽然您可以轻松编写脚本来删除2天的文件夹:

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 . 参考: %TIMESTAMP%语法


WinSCP does not require to be installed . WinSCP不需要安装 All you need to get the batches working is to extract a contents of WinSCP portable executables package along the batch file. 使批处理工作所需的只是从批处理文件中提取WinSCP可移植可执行程序包的内容。


(I'm the author of WinSCP) (我是WinSCP的作者)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM