一个批处理文件,如果有10个以上,它将删除最新的文件夹 [英] A batch file that will delete the newest folder if there's more than 10

查看:146
本文介绍了一个批处理文件,如果有10个以上,它将删除最新的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个批处理文件(.bat),如果有10个以上的文件夹,则可以删除文件夹中的最新文件夹.如果这是目录树:

I want to create a batch file (.bat) that can delete the newest folder in a folder of folders if there's more than 10 folders. If this is the directory tree:

\My Folders\
... \Old Videos\        (Created 2 Hours Ago)
... \Files\             (Created Last Month)
... \Pics\              (Created Last Week)
... \Powerpoints\       (Created Yesterday)
... \Videos\            (Created Several Days Ago)
... \Old Pics\          (Created Yesterday)
... \Movies\            (Created Yesterday)
... \Games\             (Created Yesterday)
... \Old Files\         (Created Last Month)
... \Downloads\         (Created Two Weeks Ago)
... \New Folder\        (Created Just Now)

因此,此批处理文件在运行时将删除新建文件夹"(位于底部),因为该文件夹中有10个以上的文件(不包括子文件夹),并且它是最新的.如果目录树中有12个文件夹(又是负子文件夹),则批处理文件将删除2个最新文件夹,依此类推.

So this batch file, when run, will delete New Folder (at the bottom) because there is more than 10 files in the folder (excluding subfolders), and it is the newest. If there was 12 folders in the directory tree (again, minus subfolders), the batch file would delete the 2 newest, and so on.

在Win10中有没有办法做到这一点?我已经把它弄乱了,但还没能做到.

Is there a way to do this in Win10? I've messed around with it a bit, but haven't been able to do it.

推荐答案

dir有很多有用的开关:
/ad仅显示文件夹(无文件)
/od按日期排序(最旧的优先)
/tc采用创建的"日期时间(默认值:最后更改")
/b仅显示名称.

dir has a lot of useful switches:
/ad to show folders only (no files)
/od to sort by date (oldest first)
/tc to take "created" date-time (default: "last-changed")
/b to show just the names.

放置一个for循环,跳过前10行就可以了:

Put a for loop around, skip the first 10 lines and you're done:

@echo off
cd "My Folders"
for /f "skip=10 delims=" %%a in ('dir /ad /od /tc /b') do ECHO rd /s /q "%%a"

如果输出是您想要的,请删除ECHO.

remove the ECHO if the output is what you want.

这篇关于一个批处理文件,如果有10个以上,它将删除最新的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆