简体   繁体   English

每24小时删除目录中的所有文件,但不删除该目录

[英]Delete all files in a directory but not the directory every 24 hours

I am looking to do some java, that will delete all the contents of a directory every 24 hours, what is the best approach to doing this? 我正在寻找一些Java,它将每24小时删除一次目录中的所有内容,最好的方法是什么? I have found a lot of ways using php but not java. 我发现使用php而不使用java的方法很多。

what is the best aproach to doing this 最好的方法是什么

Assuming that you really need to do this in Java: 假设您确实需要在Java中执行此操作:

  1. Write a java method that iterates over the files in a directory using (for example) File.listFiles() . 使用(例如) File.listFiles()编写一个遍历目录中文件的java方法。 For each one call File.delete() . 对于每个调用File.delete() (If the directory contains subdirectories, it will be a bit more complicated ... depending on what you want to happen for the subdirectories. (如果目录包含子目录,则要复杂一些……取决于要对子目录执行的操作。

  2. Write some java code that calls the previous method once every 24 hours. 编写一些每24小时调用一次以前方法的Java代码。

References: 参考文献:

Alternatively, you might be able to find some existing code using Google. 另外,您也许可以使用Google查找一些现有代码。 (But I'm not going to look for you.) (但我不会去找你。)

I know you said java ... but.. If you are using unix use crontab 我知道您说过Java ...但是..如果您使用的是UNIX,请使用crontab

crontab -e

0 1 * * * rm -rf /path/to/mydir/*

Delete all files in your dir at 1am every night 每晚凌晨1点删除目录中的所有文件

And in a windows machine you can: 在Windows机器中,您可以:

at 00:00 /every:M,T,W,Th,F,S,Su "cmd /c del /f/s/qc:\\path\\to\\dir"

Which deletes everything (including subdirectories) in c:\\path\\to\\dir quietly at 00:00 (midnight) everyday. 每天每天00:00 (午夜)静默删除c:\\path\\to\\dir所有内容(包括子目录)。

Use at /delete to disable the scheduled tasks. at /delete使用可禁用计划的任务。

You can read more about the At Utility here . 您可以在此处阅读有关At Utility的更多信息。

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

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