Amateur batch code seeks help – Uninstall via WMIC and product ID.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tonyhes
Posts: 1
Joined: 12 Apr 2016 10:50

Amateur batch code seeks help – Uninstall via WMIC and product ID.

#1 Post by tonyhes » 12 Apr 2016 13:23

Hello Everyone, (My 1st post!)

I am trying to create a batch will universally uninstall iTunes.
The idea is to search for the product ID of iTunes and if present uninstall via that product ID.



Here is the code I have so far:

for /f "delims==" %%f in ('WMIC PRODUCT WHERE "Name like "%%iTunes%%"" get IdentifyingNumber ^| FIND /V "IdentifyingNumber" ^| FIND " "') do set "prodid=%%f"
echo %prodid%

if "%prodid%" NEQ "No Instance(s) Available." msiexec /x "%prodid%" /q

The result:

***I believe the problem is that somehow 2 spaces/blank char is included at the end of my prodid variable.***


C:\Temp>for /F "delims==" %f in ('WMIC PRODUCT WHERE "Name like "%iTunes%"" get IdentifyingNumber | FIND /V "IdentifyingNumber" | FIND " "') do set "prodid=%f"

C:\Temp>set "prodid={FBEB98F8-64E4-4FA3-A15E-4A9F42FF962E}
"

C:\Temp>echo {FBEB98F8-64E4-4FA3-A15E-4A9F42FF962E}
{FBEB98F8-64E4-4FA3-A15E-4A9F42FF962E}

C:\Temp>if "{FBEB98F8-64E4-4FA3-A15E-4A9F42FF962E} " NEQ "No Instance(s) Available." msiexec /x "{FBEB98F8-64E4-4FA3-A15E-4A9F42FF962E} " /q
T h i s i n s t a l l a t i o n p a c k a g e c o u l d n o t b e o p e n e d . V e r i f y t h a t t h e p a c k a g e e x i s t s a n d t h a t y o u c a n a c c e s s i t , o r c o n t a c t t h e a p p l i c a t i o n v e n d o r t o v e r i f y t h a t t h i s i s a v a l i d W i n d o w s I n s t a l l e r p a c k a g e .

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Amateur batch code seeks help – Uninstall via WMIC and product ID.

#2 Post by aGerman » 12 Apr 2016 15:42

The outputted lines of WMIC contain trailing carriage return characters. You may read that thread if you want to know something about it and why I processed the output again in a second FOR /F loop:
WMIC and FOR /F : A fix for the trailing <CR> problem

Code: Select all

for /f "tokens=2 delims==" %%i in (
  'wmic product WHERE "Name LIKE '%%iTunes%%'" GET IdentifyingNumber /value'
) do for /f %%j in ("%%i") do (
  echo %%j
  msiexec /x "%%j" /q
)

No need to check if WMIC failed. "No Instance(s) Available." was outputted to the StdErr stream which will not be processed by the FOR /F loop.

Regards
aGerman

Post Reply