获取上次创建的目录批处理命令 [英] Get last created directory batch command
问题描述
如何在目录中获取最新的子文件夹?
我在 MKLINK/D
命令中需要它.
How can i get newest subfolder in directory ?
I need it in MKLINK /D
command.
谢谢
推荐答案
FOR /F "delims=" %%i IN ('dir /b /ad-h /t:c /od') DO SET a=%%i
echo Most recent subfolder: %a%
(%i
for windows 10)
(%i
for windows 10)
/b
用于裸格式/ad-h
只有目录,而不是隐藏的目录t:c
表示使用创建日期进行排序(使用t:w
为最后写入日期)/od
先排序最旧for/F
执行命令并设置a为目录名,最后一个是最新的.
/b
is for bare format/ad-h
only directories, but not the hidden onest:c
means to use the creation date for sorting (uset:w
for last write date)/od
sort oldest first- The
for /F
executes the command and sets a to the directory name, the last one is the newest one.
如果您直接在命令行(而不是在批处理文件中)执行此操作,请使用 %
而不是 %%
.
If you execute this directly on the command line (not in a batch file), use %
instead of %%
.
这适用于当前目录 - 正如@iesou 指出的,如果您需要使用任何其他目录路径,则需要在 dir
之后添加目录路径.
This works with the current directory - as @iesou pointed out you'll need to add the directory path after dir
if you need to use any other directory path.
具有指定目录路径的示例:
Example with specified directory path:
FOR /F "delims=" %%i IN ('dir "c:Program Files" /b /ad-h /t:c /od') DO SET a=%%i
<小时>
为了防止遍历所有子文件夹,您可以将排序顺序更改为最近的在前 (/o-d
) 并在第一次调用后退出 for 循环:
To prevent going through all subfolders, you may change the sort order to have the most recent first (/o-d
) and exit the for loop after the first call:
@echo off
FOR /F "delims=" %%i IN ('dir /b /ad-h /t:c /o-d') DO (
SET a=%%i
GOTO :found
)
echo No subfolder found
goto :eof
:found
echo Most recent subfolder: %a%
这篇关于获取上次创建的目录批处理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!