Open one of a series of files using a batch file

batch-file, cmd, command-line, dos

Solution

This method uses the actual file modification date, to figure out which one is the latest file:

@echo off
for /F %%i in ('dir /B /O:-D *.txt') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "dummy" "%~1"
exit /B 0

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:

@echo off
for /F %%i in ('dir /B *.txt^|sort /R') do (
    call :open "%%i"
    exit /B 0
)
:open
    start "dummy" "%~1"
exit /B 0

You actually have to choose which method is better for you.

Problem

I have up to 4 files based on this structure (note the prefixes are dates) - 0830filename.txt - 0907filename.txt - 0914filename.txt - 0921filename.txt I want to open the the most recent one (0921filename.txt). how can i do this in a batch file? Thanks.

Original source