Help writing DOS script to get get name of the most recent directory (time created)

天大地大妈咪最大 提交于 2019-12-18 12:45:50

问题


Hey Guys, I need some help in getting the name of the most recent directory in a DOS script.

I have found some information on getting the most recent file which works but I cannot get this to work on directories.

For example,Here is my directory:

drwxr-xr-x 2 usrpm Domain Users 0 Jun 29 10:34 _200903_V20
drwxr-xr-x 2 usrpm Domain Users 0 Jun 29 10:35 _200904_V21
drwxr-xr-x 2 usrpm Domain Users 0 Jun 29 10:36 _200905_V22
drwxr-xr-x 2 usrpm Domain Users 0 Jun 29 10:38 _200906_V23

I need my script to return me the most recent directory (V23). I will then cd into that directory and copy a file out of it.

Thanks for any help!


回答1:


Here is a link to two scripts that find the most recent file. I think the second one already does exactly what you want, but you can modify one of them to do what you need, I'm pretty sure. I just googled "find most recent file dos batch file" and found it immediately.

Source link.

Edited to add a script that works with directories:

@echo off
for /f "delims=" %%x in ('dir /od /b *.*') do set recent=%%x
echo %recent%

Output:

C:\> recent.bat
recent.bat
C:\> mkdir newdir

C:\> recent.bat
newdir

Looks like it works here.




回答2:


This should work:

for /f "usebackq delims=" %%i in (`dir /ad /o-d /b`) do (
    set LETESTDIR=%%i
    goto cont
)

:cont
echo %LETESTDIR%


来源:https://stackoverflow.com/questions/1724278/help-writing-dos-script-to-get-get-name-of-the-most-recent-directory-time-creat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!