windows - Batch File to get most recent file where filename contains a substring -
i'm trying write batch file open recent session log logs file.
i have bash script want on linux, listing contents of logs folder in time order, grep lines starting session , taking recent one. after append onto file path , open it. code bash script follows:
mostrecentlogfile=~/my/logs/folder/$(ls -t ~/my/logs/folder | grep ^session | head -1) displayfunction="gedit" echo "opening $mostrecentlogfile ..." $displayfunction $mostrecentlogfile
however need convert script batch file same thing on windows.
i can go far opening recent file in logs folder, there other types of log files , need recent 1 begins string "session" code doesn't open correct file:
cd my\logs\folder /f %%i in ('dir *.* /b /o:-d') notepad.exe %%i
any suggestions on how edit code filter filenames ones containing substring appreciated.
this isn't best approach, seems work
for /f %%i in ('dir *.* /b /o:d') echo %%i |>nul findstr /b "session" && set logfile=%%i notepad.exe %logfile%
instead of sorting in reverse order (newest first), sorted in standard order (oldest first), final assignment of %logfile%
newest filename starts "session". after first line %logfile%
contains filename of logfile, second line opens it.
edit:
depending on exact use case, seems shortened like
for /f "delims=" %%i in ('dir session* /b /o:d /a:-d') set "logfile=%%i" notepad "%logfile%"
Comments
Post a Comment