Link to home
Start Free TrialLog in
Avatar of JB Blanco
JB BlancoFlag for United States of America

asked on

Need a Batch script that will check the version of a file-and Uninstall if exist

Basically i need a batch script that will first check if a particular file version exists.,  And if it does, it will uninstall it.  If it does'nt exist, than it will do nothing and exit.

in this case its the file named "BmaSearch.exe"




Here is a work in progress script that i put together.   I just need help with the check version part of it.


"@echo off

REM check if Barracuda Add-In is already installed
if exist "C:\Program Files (x86)\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe" (
GOTO UNINSTALL
) else (

if exist "C:\Program Files\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe" (
GOTO UNINSTALL
))




:UNINSTALL

REM UINSTALL ADDON
echo UnInstalling
msiexec /x \\mydomain.local\sysvol\mydomain.local\Policies\{B7D55D1D-7D20-46EB-92F9-FEB42BABC41E}\Machine\Applications\BmaOutlookAddIn-3.6.20.0_x86.msi /quiet

exit"


Please help

thanks
Avatar of Jon Covalt
Jon Covalt
Flag of United States of America image

From what I can see, this should work as you intend, with one exception; the way the code is written, it'll always try to uninstall. You're missing a statement to skip to the end if it doesn't find those files.

"@echo off

REM check if Barracuda Add-In is already installed
if exist "C:\Program Files (x86)\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe" (GOTO UNINSTALL) else (
if exist "C:\Program Files\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe" (GOTO UNINSTALL)) else (
GOTO END )




:UNINSTALL

REM UINSTALL ADDON
echo UnInstalling
msiexec /x \\mydomain.local\sysvol\mydomain.local\Policies\{B7D55D1D-7D20-46EB-92F9-FEB42BABC41E}\Machine\Applications\BmaOutlookAddIn-3.6.20.0_x86.msi /quiet

:END

exit"
Avatar of oBdA
oBdA

This should do the trick; it's currently in test mode and will only display the uninstall command it would normally run.
Remove the uppercase ECHO on front of msiexec to run it for real.
@echo off
setlocal

call :GetFileVersion FileVersion "C:\Program Files (x86)\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe"
if "%FileVersion%"=="3.6.20.0" goto Uninstall
call :GetFileVersion FileVersion "C:\Program Files\Barracuda\Message Archiver\Outlook Add-In\BmaSearch.exe"
if "%FileVersion%"=="3.6.20.0" goto Uninstall
echo BmaSearch.exe not found in a version that needs uninstalling.
goto :eof

:Uninstall
echo Uninstalling Barracuda Add-In ...
ECHO msiexec.exe /x "\\mydomain.local\sysvol\mydomain.local\Policies\{B7D55D1D-7D20-46EB-92F9-FEB42BABC41E}\Machine\Applications\BmaOutlookAddIn-3.6.20.0_x86.msi" /quiet


goto :eof
:GetFileVersion <ByRef Variable> <Path>
set __FilePath=%~2
if not exist "%__FilePath%" (
	set %1=
	goto :eof
)
set TempFile=%Temp%\wmic.tmp
wmic.exe DataFile WHERE Name='%__FilePath:\=\\%' GET Version /format:list >"%TempFile%"
for /f "delims=" %%a in ('type "%TempFile%" ^| find.exe "="') do (
	for /f "tokens=1* delims==" %%u in ("%%a") do set __FileVersion=%%v
)
echo '%~2': %__FileVersion%
set %1=%__FileVersion%
goto :eof

Open in new window

Avatar of JB Blanco

ASKER

Thanks oBdA!

just a quick question,

goto :eof
:GetFileVersion <ByRef Variable> <Path>
set __FilePath=%~2
if not exist "%__FilePath%" (
      set %1=
      goto :eof
)
set TempFile=%Temp%\wmic.tmp
wmic.exe DataFile WHERE Name='%__FilePath:\=\\%' GET Version /format:list >"%TempFile%"
for /f "delims=" %%a in ('type "%TempFile%" ^| find.exe "="') do (
      for /f "tokens=1* delims==" %%u in ("%%a") do set __FileVersion=%%v
)
echo '%~2': %__FileVersion%
set %1=%__FileVersion%
goto :eof

what does all this do?

is this really necessary?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS!!!  that script worked perfectly!!!

wow!!  

I wish i could come up with stuff like that!