Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the last directory created in batch [duplicate]

This is my first question and I'm not very experienced using batch files so hope someone can help.

I want to find the last directory created using a batch file and have tried:

FOR /f "tokens=*" %%A in ('dir "%latestdirectory%" /AD-h /B /o-d') do (set recent=%%A)

but this result keeps returning the oldest directory not the most recent one.

Still trying to pick this up in batch.

like image 428
danielg110 Avatar asked Jun 21 '13 14:06

danielg110


People also ask

What does %1 do in 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 current directory in a batch file?

It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3 , then run the batch, the current directory will be c:\dir3 .

What Is REM copy in batch file?

Purpose: Provides a way to insert remarks (that will not be acted on) into a batch file. Discussion. During execution of a batch file, DOS will display (but not act on) comments which are entered on the line after the REM command.

What does Colon do in batch?

By adding a colon in front of a word, such as LABEL, you create a category, more commonly known as a label. A label lets you skip to certain sections of a batch file such as the end of the batch file.


2 Answers

FOR /f "delims=" %%A in ('dir "%latestdirectory%" /AD-h /B /od') do (set recent=%%A)

for help enter dir /? at the command line.

like image 110
Endoro Avatar answered Oct 13 '22 18:10

Endoro


To get the last created subdirectory (and not the last modified one if any file or sub-sub-directory added in it), this should work:

FOR /F %%i IN ('dir /a:d /t:c /o-d /b') DO (
    SET a=%%i
    GOTO :found_last
)

echo No subfolder found
goto :eof

:found_last
echo Most recent subfolder: %a%
set last_subforlder=%a%
like image 29
Farah Avatar answered Oct 13 '22 20:10

Farah