On my hard disk, I have hundreds subfolders created daily that may contain a specific text file (Tracker.txt).
I would like to create a cmdline or BAT file to get only one result, the latest tracker.txt file, with its full path !
I tried dir /s tracker.txt /OD
but the result is not filtered by date.
Thank you!
Best Answer
You can pipe the
DIR /B /S /OD "tracker.txt"
command results to a temp log file but have it use theFOR
loop substitutions to put the date time stamp of each file that matches the name followed by a comma and then the full explicit path and file name (i.e.YYYY-MM-DD hh:mm [AM/PM]
).You then use the
SORT
command against that file to sort all the lines in that file to a new sorted file but with the newest time stamp file at the bottom of the list.Lastly, you'd run a final
FOR
loop through the final file list using the comma as the delimiter and then only setting the iterator variable in that loop to each full explicit path and when it get to the bottom of that list, this is the newest file that isSET
last and then you can do something with that last set variable which is the newest file date stamp wise from within all the directories you specify recursively.Batch Script Example
Be sure to set the value of the
SET StartDir=
variable to be the root folder where the DIR command starts for finding the files recursively.Further Resources
FOR /?
(batch substitutions)