更新时间:2022-10-20 19:00:19
FOR / F在('DIR / B / AD-H跳过= 2 delims =%G / O -d')DO回声要删除%G
FOR / F
从处理输出行单引号
DIR ...
Delims =
更好。 标记= *
将在这里工作,但SUP presses前导空格。
/ B
来燮preSS头输出 DIR
/ O-D
的反向日期顺序。你想保留两跳过迪尔斯,在反向日期排序发生的FIRS。
...好回声
的安全措施。
I want to delete all the folders from a directory except the latest 2 folders (Last modified date). I want to do this by a small command line code. How do I do it? Below is the code to display folders sorted by last modified time.
dir /ad-h /od
I tried using FOR /D
command.
for /d "skip=2 tokens=*" %G in ("dir /ad-h /od") DO echo going to delete %G
But I got the below error message.
"skip=2 tokens=*" was unexpected at this time.
How do I do it?
for /f "skip=2 delims=" %G in ('dir /B /ad-h /o-d') DO echo going to delete %G
For /f
to process lines of "output" from single-quoted
DIR...
Delims=
is better. tokens=*
will work here, but suppresses leading spaces.
/b
to suppress header output from dir
/o-d
for REVERSE-date order. You want to KEEP the two skipped dirs, which occur FIRS in a reverse-date sort.
...good echo
safety measure.