Complete Communications Engineering

This depends on exactly what “Latest Version” means.  One possibility is that a directory contains multiple versions of the same file, and the goal is to find one of them.  It could be the one with the most recent timestamp, or if the file names have version numbers it could be the one with the highest version number.  Both of these are possible as the following example demonstrates:


@echo off




for /f “tokens=*” %%f in (‘dir /b version*.txt’) do set recent_file=%%f


echo Recent file (version number): %recent_file%


for /f “tokens=*” %%f in (‘dir /b /tw /ot file*.txt’) do set recent_file=%%f


echo Recent file (write time): %recent_file%

The first for command in this example lists all .txt files beginning with ‘version’.  The ‘/b’ switch changes the output of the dir command so only the filenames are printed.  Each file found is assigned to the recent_file variable.  By default, the dir command will list files in alphabetical order.  If the version numbers conform to that order, then the latest version number will be assigned to recent_file last and it will end up with that file name as its value.  The second for command uses two other switches for the dir command.  The ‘/tw’ switch tells dir to use the write time attribute of each file.  The ‘/ot’ switch tells dir to sort the results by time.  With this command, the file with the most recent write time will be printed last, and end up as the value of recent_file.