1

I have a directory with many subdirectories. Each subdirectory is named with the yyyy-mm-dd format. I'm writing a Windows batch file and I need to grab the directory name with the most recent date and put that string into a variable. The last modified dates were changed, so using the last modified timestamp isn't an option.

Can anyone point me in the right direction?

2
  • Any chance you can consider upgrading to powershell instead of a batch file? The Sort-Object commandlet would make this trivial.
    – Zoredache
    Apr 9, 2019 at 0:21
  • @Zoredache that might work if I can make a call to PowerShell to grab the folder name and return it to the batch script?
    – slantalpha
    Apr 9, 2019 at 0:46

1 Answer 1

3
for /f %%d in ('dir /ad /o-n /b .') do (
  echo %%d
  goto break
)
:break
  • /ad - Displays files with specified attributes. (D: Directories)
  • /o-n List by files in sorted order. (n: By name (alphabetic), -n: Prefix to - reverse order)
  • /b Uses bare format (no heading information or summary).
  • goto break - jump out the loop to take only first

ps

  • /o-d List by files in sorted order. (d: By date/time (oldest first), -d: Prefix to - reverse order)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .