Home > database >  Trying to get the last created/modified subfolder in folder
Trying to get the last created/modified subfolder in folder

Time:03-26

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:

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

CodePudding user response:

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.

  • Related