Jump to content

Batch files - Check file versions


rkillcrazy

Recommended Posts

I hope this belongs in this forum... It deals with most other NT OSs as well.

I'm a network/PC admin and I deal with a lot of cheap customers! One of them won't take my advise and continues to purchase Dells with XP Home! Nonetheless, I still have to manage 80-100 (to date) XP PCs that should be on a domain. I repack a lot of installs and set up batch files to I can go from seat-to-seat and just make a few clicks to install the same old software. I have the batch files set up to use CHOICE.EXE & CHOICE.COM (I had to get those since XP doesn't have them) to allow me to choose what I want to install. I still have to sit there and hit "Y" or "N" but it's better than answer all the other stuff like EULAs and install paths. OK, venting all done......

My question:

Is there a way to have a batch file check the version of a file? Example: The PC has Spybot 1.3 and I wish to install v1.4. How can I set up the batch file to check the version number, and with an %ERRORLEVEL% EQL/LEQ/GEQ statement, install v1.4?

05-14-07

1146 EDT

Link to comment
Share on other sites


There is no built-in command line utility for determining a files version. And I don't know of any third party utility that does this. However, you might be able to get away with using the "find" command.

 echo   *** Checking For Latest Version of SpyBot
SET CHECKFILE=%ProgramFiles%\Spybot - Search & Destroy\spybot.exe
IF EXIST "%CHECKFILE%" (
ECHO - File Found, Checking Version...
FIND /I "1.4" "%CHECKFILE%" > NUL:
IF ERRORLEVEL 0 (
ECHO - Version at 1.4
) ELSE (
ECHO - Incorrect Version Found
CALL UPDATESPYBOT.CMD
)
) ELSE (
ECHO - Spybot not found, Installing...
CALL UPDATESPYBOT.CMD
)

I just wrote this off the top of my head. It probably contains bugs, but you get the general idea. The trick is to use the "find" command and errorlevel checking.

Edited by Nois3
Link to comment
Share on other sites

There is no built-in command line utility for determining a files version. And I don't know of any third party utility that does this. However, you might be able to get away with using the "find" command.

 echo   *** Checking For Latest Version of SpyBot
SET CHECKFILE=%ProgramFiles%\Spybot - Search & Destroy\spybot.exe
IF EXIST "%CHECKFILE%" (
ECHO - File Found, Checking Version...
FIND /I "1.4" "%CHECKFILE%" > NUL:
IF ERRORLEVEL 0 (
ECHO - Version at 1.4
) ELSE (
ECHO - Incorrect Version Found
CALL UPDATESPYBOT.CMD
)
) ELSE (
ECHO - Spybot not found, Installing...
CALL UPDATESPYBOT.CMD
)

I just wrote this off the top of my head. It probably contains bugs, but you get the general idea. The trick is to use the "find" command and errorlevel checking.

That looks simple enough although I've never used the FIND command. I'm curious as to where it searches for "1.4." Is it looking in the file name or in some "properties" value that you'd normally see if you right-clicked the file, went to PROPERTIES and looked at the VERSION tab?

I'll even take suggestions using 3rd party utilities as long as I can bundle the simple EXE or COM files with the installers. I'm not overly familiar with VB scripting but maybe that's a way around this. There must be a way of doing it since many installers are able to identify a newer or older version of their own apps during the install or upgrade.

Thanks for the quick reply btw!

05-14-07

1311 EDT

Link to comment
Share on other sites

Before gunsmokingman comes up with a vbscript/jscript solution, here is a batch file example using WMIC.

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS

::USER VARIABLES
::FILE TO CHECK VERSION OF [Note double backlashes]
SET "F_=C:\\Program Files\\MSN Messenger\\msnmsgr.exe"
::NEW FILE VERSION
SET "C_=7.5.0324.00"

FOR /F "USEBACKQ SKIP=1" %%? IN (
`WMIC DATAFILE WHERE "NAME='%F_%'" GET VERSION 2^>NUL`) DO (
IF NOT ERRORLEVEL 1 (CALL :CHK_ %%?))
PAUSE &ENDLOCAL &GOTO :EOF
:CHK_
SET "N_=%1"
IF %N_:.=% LSS %C_:.=% (ECHO/Install v%N_% Replacing v%C_%) ELSE (
ECHO/Existing File Is Already v%N_% And Does Not Require Updating)

Link to comment
Share on other sites

Before gunsmokingman comes up with a vbscript/jscript solution, here is a batch file example using WMIC.
@ECHO OFF &SETLOCAL ENABLEEXTENSIONS

::USER VARIABLES
::FILE TO CHECK VERSION OF [Note double backlashes]
SET "F_=C:\\Program Files\\MSN Messenger\\msnmsgr.exe"
::NEW FILE VERSION
SET "C_=7.5.0324.00"

FOR /F "USEBACKQ SKIP=1" %%? IN (
`WMIC DATAFILE WHERE "NAME='%F_%'" GET VERSION 2^>NUL`) DO (
IF NOT ERRORLEVEL 1 (CALL :CHK_ %%?))
PAUSE &ENDLOCAL &GOTO :EOF
:CHK_
SET "N_=%1"
IF %N_:.=% LSS %C_:.=% (ECHO/Install v%N_% Replacing v%C_%) ELSE (
ECHO/Existing File Is Already v%N_% And Does Not Require Updating)

OK, WMIC is new to me. I've been messing around with more and more command-line code these days but I'll admit - I've never seen this before! :blink:

I've seen "FOR" statements but never really used them. Same with "%1" & "%%" for that matter... This looks like it's certainly plausible. A little background as far as what's going on in the code would be nice, if you don't mind. This would help me to understand what's happening. It looks like WMIC is also used in Vista which should make it easier on me as I see more and more of these machines.

Nonetheless, I'm willing to give this a try. All I'll need is some help with understanding a new language.

Link to comment
Share on other sites

Spybot is simple because each version is in a differently named folder. The "If Exist" command could surely be used to determine what folder is already installed.

However, other programs do NOT rename the folder when they go from version to version, so some other means would have to be used, like looking for a particular file in the programs folder that would be peculiar to a specific version. Then every time they do an upgrade to a different version, you've got to re-write your batch files. Bummer!

I do this stuff all the time, loading upgraded software, etc. into customers PC's.

I just look to see what's already there and load what's needed from my Utilities CD or Flash Drive.

I never thought about automating it further. The very same batch file can also be used to delete the old version.

I do use lots of little batch files to install programs and copy the latest updates to their folders. It sure saves a lot of time.

Good Luck,

Me B)

Link to comment
Share on other sites

Windows XP Service Pack 2 Support Tools has a program called filever.exe.

filever C:\WINNT\NOTEPAD.EXE

----- W32i APP ENU 5.1.2600.2180 shp 69,120 08-04-2004 notepad.exe

You could pipe the output of the filever command into a find or grep (part of UnxUtils) then use the returned errorlevel. The problem with this method is you can only search of a direct match.

You could also write a vbs script and call it using cscript from your batch file.

Here is a sample script I wrote. If the program was t.vbs call it using "cscript -nologo t.vbs". It is also possible to remove the Run command from the function and use the return value to control what is run. Anyway it's not perfect but its a good starting point.

function compare(strFile, strVer, srtRun)

dim arrVer, arrInst, intCount

if NOT fso.FileExists(strFile) then WScript.Echo strFile & " dosen't exist" : compare=false : exit function

arrVer=Split(strVer, ".")

arrInst = Split(fso.GetFileVersion(strFile), ".")

intCount = UBound(arrVer) : if UBound(arrVer) > UBound(arrInst) then intCount = UBound(arrInst)

for i=0 to intCount

if CInt(arrVer(i)) < CInt(arrInst(i)) then

WScript.Echo "Installed version is newer"

compare=false

exit function

end if

if CInt(arrVer(i)) > CInt(arrInst(i)) then

WScript.Echo "Installed version is older"

rem http://www.devguru.com/Technologies/wsh/qu...hshell_Run.html

WshShell.Run srtRun, 1, TRUE

compare=true

exit function

end if

next

WScript.Echo "Installed version is the same"

compare=false

exit function

end function

Set WshShell = WScript.CreateObject("WScript.Shell")

Set fso = WScript.CreateObject("Scripting.FileSystemObject")

z=compare ("C:\Notepad.exe","5.1.2600.2181","cmd.exe")

z=compare ("C:\WinNT\Notepad.exe","5.1.2600.2181","cmd.exe")

Link to comment
Share on other sites

Filever doesn't have an option to just display version information. You could use sed or awk which are unix commands and just filter the portion you want. There are ports of these commands in the GNU utilities for Win32. With sed you would do something like the following.

filever c:\WINNT\system32\notepad.exe | sed -e "s/.* \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*/\1/"

You could also execute the filever command within a for loop and extract the portion you want.

If you want a simple solution you can use the following script filever.vbs. It takes one input the name of the file and outputs the version of the file. If the files doesn't exist or you don't specify a file name it doesn't output anything.

'filever.vbs

Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Set objArgs = WScript.Arguments

if objArgs.Count<>1 then WScript.Quit()

if NOT fso.FileExists(objArgs(0)) then WScript.Quit()

WScript.Echo fso.GetFileVersion(objArgs(0))

to call the script just use

cscript -nologo filever.vbs "c:\WINNT\system32\notepad.exe"

Link to comment
Share on other sites

  • 2 weeks later...
All I'll need is some help with understanding a new language.

Hi, I used batch file(s) for silent fully automatic installs too. But finding versions and eventually reinstalling them was too much work for me ... so i started using NSIS

advantages

- You can create one exe file that will give you options of what you want to install

- You have to carry just one file instead of .bat and bunch of installers

- NSIS has great plugin support, so if you are looking for function thats not there already you will find it on nsis forums (i just needed plugin to check if app being installed is running or not)

disadvantage

- you will have to learn new language. But it is scripted and quite easy. And nsis comes with nice help

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...