Solved Windows batch script to find newest version of a program

April 27, 2015 at 00:57:54
Specs: Windows 7
I have several versions of the same program, located on different folders on my c-drive. I need a script that will search for the newest version (file date), and give me the full path to that version.

I have tried to use the command dir /tc /s c:\program.exe > program.txt, which gives me a list of paths and dates. I have been able to clean this list, give a line for each version, and thought I could sort it in a way, with no success.


See More: Windows batch script to find newest version of a program

Report •


✔ Best Answer
April 29, 2015 at 09:59:09
Well, I found the bug due to the name of the program being part of the name of a directory (program.exe -> program files). Here the patch:

Replace find /I "%prog%" with find /I "%prog%%ext%"
Replace dir /B /L /S with dir /A-D /B /L /S

I hope the above fix wipes out the issue. Anyway my warranty never ends.

message edited by IVO



#1
April 27, 2015 at 06:11:17
Here the solution, almost I hope; replace the variable prog with the name of your program and ext with its extension without the dot and you are done.

IMPORTANT NOTICE: the script works if and only if the /TC date is expressed as YYYY/MM/DD otherwise a substring manipulation is required to obtain the correct sorted output. If so please post the format of the date and I shall return the modified script.

@echo off & setlocal

set prog=Here the file name
set ext=Here the file extension (no dot .)

for /F "delims=" %%i in ('dir /B /L /S "C:\%prog%.%ext%"') do (
  for /F "tokens=1,2" %%j in ('dir /TC "%%i"^|find "%prog%"') do echo.%%j %%k %%i
) >> "%prog%.tmp"
sort /R "%prog%.tmp" /O "%prog%.txt" 
del "%prog%.tmp"
set /P newest=< "%prog%.txt"
echo.
echo.  %newest%

message edited by IVO


Report •

#2
April 27, 2015 at 21:59:04
Thank you again, IVO! The script seems to work. However, my /TC date format is DD.MM.YYYY. The script shall work across different PCs, so other formats may also be actual, such as DD/MM/YYYY, YYYY.MM.DD. So, I maybe need to temporaily set the format before the script is running, and then change it back afterwards. How?

message edited by IvarH1969


Report •

#3
April 28, 2015 at 13:32:13
Here the script revised to fit your needs. It detects by itself if date is YYYYMMDD or DDMMYYYY no matter what is the delimiter / . or .... If you code on the command tail a filename, it looks for that starting from the current drive root directory. If no parameters, the coded default DRIVE PROG EXT are used. Pay attention now you have to enter the extension prefixed by its dot (e.g. .exe). I tested the script and it worked fine, but please report any issue as it is quite complex. Beware it allows only the date format YMD and DMY (almost for now). Good work.

@echo off & setlocal EnableDelayedExpansion
:: [Default Data to be modified according to your needs]
set drive=C:
set prog=list
set ext=.asc
:: [End Of Default Data]
if not "%~1"=="" for %%i in ("%~1") do (set drive=%%~di& set prog=%%~ni& set ext=%%~xi)
echo.& echo.  Looking for "%prog%%ext%"...
dir /B /L /S "%drive%\%prog%%ext%" 2> nul 1> "@%prog%.txt"
if errorlevel 1 (echo.  FILE NOT FOUND & del "@%prog%.txt" & goto :END)

set flag=1 & set DMY=1
for /F "delims=" %%i in ('type "@%prog%.txt"') do (
  for /F "tokens=1,2" %%j in ('dir /TC "%%i"^|find /I "%prog%"') do (
    set TCdate=%%j
    if defined flag (set flag=& for /L %%k in (0 1 9) do if "!TCdate:~2,1!"=="%%k" set DMY=)
    if defined DMY (set TCdate=!TCdate:~6,4!/!TCdate:~3,2!/!TCdate:~0,2!)
    echo.!TCdate! %%k %%i
  )
) >> "@%prog%.tmp"
sort /R "@%prog%.tmp" /O "@%prog%.txt" 
del "@%prog%.tmp"
set /P newest=< "@%prog%.txt"
echo.  Most recent release is %newest%
echo.  Full list in "@%prog%.txt"
:END
echo.& echo.  Press any key to Quit... & pause > nul


Report •

Related Solutions

#4
April 28, 2015 at 23:56:30
Thank you again for help. For some reason, the script gives the following ouput:

ory/ec/Di of c:\program files\vendor\program v3\bin\program.exe
ory/ec/Di of c:\program files (x86)\vendor\program v2\bin\program.exe
ory/ec/Di of c:\program files (x86)\vendor\program v1\bin\program.exe
2014/12/27 15:56 c:\program files\vendor\program v3\bin\program.exe
2014/09/23 09:26 c:\program files (x86)\vendor\program v2\bin\program.exe
2014/06/16 08:36 c:\program files (x86)\vendor\program v1\bin\program.exe

I do not understand where in the script the first three lines are created, and are not able to modify the script to remove these.


Report •

#5
April 29, 2015 at 09:59:09
✔ Best Answer
Well, I found the bug due to the name of the program being part of the name of a directory (program.exe -> program files). Here the patch:

Replace find /I "%prog%" with find /I "%prog%%ext%"
Replace dir /B /L /S with dir /A-D /B /L /S

I hope the above fix wipes out the issue. Anyway my warranty never ends.

message edited by IVO


Report •

Ask Question