<p>I have up to 4 files based on this structure (note the prefixes are dates)</p>

<ul>
<li>0830filename.txt</li>
<li>0907filename.txt</li>
<li>0914filename.txt</li>
<li>0921filename.txt</li>
</ul>

<p>I want to open the the most recent one (0921filename.txt).  how can i do this in a batch file?</p>

<p>Thanks.</p>

[{'answer_id': 51861, 'author': 'Graviton', 'author_id': 3834, 'author_profile': 'https://Stackoverflow.com/users/3834', 'pm_score': -1, 'selected': False, 'text': '<p>Use regular expression to parse the relevant integer out and compare them.</p>\n'}, {'answer_id': 51868, 'author': 'seanyboy', 'author_id': 1726, 'author_profile': 'https://Stackoverflow.com/users/1726', 'pm_score': 1, 'selected': False, 'text': '<p>Here you go... (hope no-one beat me to it...)  (You\'ll need to save the file as lasttext.bat or something) \nThis will open up / run the oldest .txt file</p>\n\n<pre><code>dir *.txt /b /od &gt; systext.bak \nFOR /F %%i in (systext.bak) do set sysRunCommand=%%i \ncall %sysRunCommand%\ndel systext.bak /Y\n</code></pre>\n\n<p>Probably XP only. BEHOLD The mighty power of DOS.<br>\nAlthough this takes the latest filename by date - NOT by filename..</p>\n\n<p>If you want to get the latest filename, change /od to /on .<br>\nIf you want to sort on something else, add a "sort" command to the second line. </p>\n'}, {'answer_id': 51875, 'author': 'Paulius', 'author_id': 1353085, 'author_profile': 'https://Stackoverflow.com/users/1353085', 'pm_score': 4, 'selected': True, 'text': '<p>This method uses the actual file modification date, to figure out which one is the latest file:</p>\n\n<pre><code>@echo off\nfor /F %%i in (\'dir /B /O:-D *.txt\') do (\n    call :open "%%i"\n    exit /B 0\n)\n:open\n    start "dummy" "%~1"\nexit /B 0\n</code></pre>\n\n<p>This method, however, chooses the last file in alphabetic order (or the first one, in reverse-alphabetic order), so if the filenames are consistent - it will work:</p>\n\n<pre><code>@echo off\nfor /F %%i in (\'dir /B *.txt^|sort /R\') do (\n    call :open "%%i"\n    exit /B 0\n)\n:open\n    start "dummy" "%~1"\nexit /B 0\n</code></pre>\n\n<p>You actually have to choose which method is better for you.</p>\n'}, {'answer_id': 51906, 'author': 'PabloG', 'author_id': 394, 'author_profile': 'https://Stackoverflow.com/users/394', 'pm_score': 2, 'selected': False, 'text': "<p>One liner, using EXIT trick:</p>\n\n<pre><code>FOR /F %%I IN ('DIR *.TXT /B /O:-D') DO NOTEPAD %%I &amp; EXIT\n</code></pre>\n\n<p><strong>EDIT:</strong></p>\n\n<p>@pam: you're right, I was assuming that the files were in date order, but you can change the command to:</p>\n\n<pre><code>FOR /F %%I IN ('DIR *.TXT /B /O:-N') DO NOTEPAD %%I &amp; EXIT\n</code></pre>\n\n<p>then you have the file list sorted by name in reverse order.</p>\n"}, {'answer_id': 51922, 'author': 'Paulius', 'author_id': 1353085, 'author_profile': 'https://Stackoverflow.com/users/1353085', 'pm_score': 3, 'selected': False, 'text': '<p>Sorry, for spamming this question, but I just really feel like posting <strong>The Real Answer</strong>.\nIf you want your BATCH script to <em>parse</em> and <em>compare</em> the dates in filenames, then you can use something like this:</p>\n\n<pre><code>@echo off\n\nrem Enter the ending of the filenames.\nrem Basically, you must specify everything that comes after the date.\nset fn_end=filename.txt\n\nrem Do not touch anything bellow this line.\nset max_month=00\nset max_day=00\n\nfor /F %%i in (\'dir /B *%fn_end%\') do call :check "%%i"\ncall :open %max_month% %max_day%\nexit /B 0\n\n:check\n    set name=%~1\n    set date=%name:~0,4%\n    set month=%date:~0,2%\n    set day=%date:~2,2%\n    if /I %month% GTR %max_month% (\n        set max_month=%month%\n        set max_day=%day%\n    ) else if /I %month% EQU %max_month% (\n        set max_month=%month%\n        if /I %day% GTR %max_day% (\n            set max_day=%day%\n        )\n    )\nexit /B 0\n\n:open\n    set date=%~1\n    set month=%~2\n    set name=%date%%month%%fn_end%\n    start "dummy" "%name%"\nexit /B 0\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51837', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/730/']