Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to get the last created/modified subfolder in folder

Environment:
Windows 2016 Server Standard
Windows command prompt and batch files

I’m trying to sort the subfolders by DateCreated or DateModified and get the latest folder name.

for %%i in ('DIR /AD /OD /B') do echo %%i

I was expecting this to give me the name of the last folder in the set. I tested the DIR command and I verified that the last subfolder is indeed I’m looking for. Instead, I get the following result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

C:\ProgramData\UPS\install\wpf>
'DIR
/AD
/OD
/B'

>Solution :

The additional option (using the /F flag) you’re looking for is "usebackq"

usebackq – specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.

So the updated command would be for /F "usebackq" %%i in (`DIR /AD /OD /B`) do echo %%i. Note that the single quotes were replaced with "back quotes" (though I’ve always called them "grave accent" or "back tick"). This makes for execute the command in-between them.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading