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.
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.
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 .
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.
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.
FOR /f "delims=" %%A in ('dir "%latestdirectory%" /AD-h /B /od') do (set recent=%%A)
for help enter dir /?
at the command line.
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With