删除Windows中除2个最新文件夹以外的所有文件夹 [英] Delete all folders except 2 latest folders in Windows

查看:126
本文介绍了删除Windows中除2个最新文件夹以外的所有文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从目录中删除除2个最近更新的文件夹(上次修改日期)以外的所有文件夹.

I want to delete all the folders from a directory except the 2 most recently updated folders (Last modified date).

我想使用命令行来做到这一点.我该怎么办?

I want to do this using the command line. How do I do it?

以下是显示按上次修改时间排序的文件夹的代码:

Below is the code to display folders sorted by last modified time:

dir /ad-h /od

我尝试使用FOR /D命令:

for /d "skip=2 tokens=*" %G in ("dir /ad-h /od") DO echo going to delete %G

但是我收到以下错误消息:

But I get the below error message:

"skip=2 tokens=*" was unexpected at this time.

我该怎么做?

推荐答案

for /f "skip=2 delims=" %G in ('dir /B /ad-h /o-d "d:\your path" ') DO echo going to delete "%G"

For /f来处理输出"行.来自single-quoted DIR ...

For /f to process lines of "output" from single-quoted DIR...

Delims=更好. tokens=*在这里可以使用,但是会取消前导空格.

Delims= is better. tokens=* will work here, but suppresses leading spaces.

/b禁止显示dir

/o-d用于REVERSE日期顺序.您想保留两个跳过的目录,它们在反向排序中以FIRS出现.

/o-d for REVERSE-date order. You want to KEEP the two skipped dirs, which occur FIRS in a reverse-date sort.

...好的echo安全措施.

...good echo safety measure.

这篇关于删除Windows中除2个最新文件夹以外的所有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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