Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last created directory batch command

Tags:

How can i get newest subfolder in directory ?
I need it in MKLINK /D command.

Thanks

like image 353
enfix Avatar asked May 09 '12 15:05

enfix


People also ask

How do I find the previous directory path?

To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

How do you go to the last directory in Command Prompt?

When you want to go back, type cd - and you will be back where you started.

What is %1 in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What is the command to present the files created ordered from oldest to most recent?

- : Prefix to reverse order. For instance, an option of "/O:D" displays files oldest-to-newest, and "/O:-S" displays files biggest-to-smallest.


1 Answers

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)

  • /b is for bare format
  • /ad-h only directories, but not the hidden ones
  • t:c means to use the creation date for sorting (use t: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 %%.

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 

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% 
like image 91
marapet Avatar answered Oct 06 '22 23:10

marapet