Need a Batchscript 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
JB BlancoSr Systems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CorinTackNetwork EngineerCommented:
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"
0
oBdACommented:
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

2
JB BlancoSr Systems EngineerAuthor Commented:
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?
0
oBdACommented:
That's a function that checks whether the file passed exists, and if so, returns the file version. You wanted a file version check, so I guess it's necessary.
Lines 4 and 6 in the script above call this function, and when it returns, the variable FileVersion will either be empty if the file could not be found, or contain the file version.
There's nothing you need to change there, it just needs to sit at the end of the script.
You can add your own code as usual as long as you stay above the "goto :eof" line before ":GetFileVersion <ByRef Variable> <Path>" (currently in line 16).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JB BlancoSr Systems EngineerAuthor Commented:
THANKS!!!  that script worked perfectly!!!

wow!!  

I wish i could come up with stuff like that!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.