Malcolm
Malcolm

Reputation: 1807

Get the latest created folder path in Windows command prompt

Is there any way I can get the latest folder name created in a specified path?

I have the below path:

C:\Test_bat\Archive

Within this path folder are dynamically created and on the run I want to get in to this folder at some point for renaming one of the file which gets created.

C:\Test_bat\Archive\NewFolder

Pseudo code of my running bat, test.bat:

set datestr= %date:~4,2%%date:~7,2%%date:~10,4%
set currtime=%time: =0%
set timestr=%currtime:~0,2%%currtime:~3,2%%currtime:~6,2%
set currdatetime=%datestr%_%timestr%
rename "C:\Test_bat\Archive\NewFolder\log.txt" "Log_%currdatetime%.txt"

Upvotes: 0

Views: 3872

Answers (1)

Malcolm
Malcolm

Reputation: 1807

Here what i got , took a refrence from below SO question :

GetNameOfRecentDirectory

Mine implementation :

set datestr= %date:~4,2%%date:~7,2%%date:~10,4%
set currtime=%time: =0%
set timestr=%currtime:~0,2%%currtime:~3,2%%currtime:~6,2%
set currdatetime=%datestr%_%timestr%

FOR /F "delims=" %%i IN ('dir "C:\Test_bat\Archive" /b /ad-h /t:c /o-d') DO (
SET a=%%i
GOTO :found
)
goto :eof
:found
rename "C:\Test_bat\Archive\%a%\ExecutionLog.txt" "Log_%currdatetime%.txt"

Upvotes: 2

Related Questions