删除Windows中除2个最新文件夹以外的所有文件夹 [英] Delete all folders except 2 latest folders in Windows
问题描述
我想从目录中删除除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屋!