用于删除目录中旧的和不同的文件夹的批处理文件
我想编写一个 DOS 批处理文件来删除目录中的旧文件夹,但保留最新的文件夹。以下是 D:\myfolders\
下的文件夹示例:
Jack_20110507 (previous day time-stamped folder)
Jack_20110508 (current day time-stamped folder)
James_20110507
James_20110508
Kenny_20110507
Kenny_20110508
...
我想删除所有前一天带时间戳的文件夹 *_20110507
,但保留所有当天时间 -带标记的文件夹*_20110508
。 每天都会创建新的带时间戳的文件夹。
I want to write a DOS batch file to delete old folders in a directory but keep the newest folder. Here is a sample of my folders under D:\myfolders\
:
Jack_20110507 (previous day time-stamped folder)
Jack_20110508 (current day time-stamped folder)
James_20110507
James_20110508
Kenny_20110507
Kenny_20110508
...
I would like to delete all previous day time-stamped folders *_20110507
, but keep all current day time-stamped folders *_20110508
. New timestamped folders are created daily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的脚本将获取 YYYYMMDD 中的当前本地日期(由此答案提供)。
然后,它循环遍历给定目录中的所有文件夹(在我的例子中,这是包含脚本的文件夹下的测试子文件夹)并检查文件夹名称的最后 8 个字符。如果它们与今天的日期不匹配,则会删除该文件夹。
/Q
禁用文件夹删除确认。如果您希望在删除文件夹之前收到确认提示,请将其删除。The script below will get the current local date in YYYYMMDD (courtesy of this answer).
It then loops through all the folders in a given directory (In my case this was a test sub folder beneath the folder containing the script) and checks the last 8 characters of the folder name. If they do not match today's date, it deletes the folder.
The
/Q
disables the confirmation for folder deletion. If you want to be prompted for confirmation before a folder is deleted, remove this.除了最简单的事情之外,我不会使用批处理脚本。
我建议您查看脚本语言,例如 Python。
I wouldn't use a batch script for anything other than the very simplest stuff.
I recommend you look at a scripting language such as Python.