By Killsek


2018-10-11 13:50:15 8 Comments

I have been trying to get a batch file to work but I have been unsuccessful; maybe someone can assist. I have a batch file that reads a text document that's in the same folder and named KB-list.txt whihc is a list of Window KB's that are to be scanned, if any of these are present on the PC I want it to uninstall.

Please find the code I created below, the search works fine its when it finds one that doesn't work it just shows that it was found.

@echo off
setlocal enabledelayedexpansion

echo "Hotfix & Updates list."
Echo.
(for /f %%a in ('type c:\KB-list.txt') do (
echo "Searching For KB%%a"
Echo.
systeminfo | findstr KB%%a

))

for /f %%i in ('type C:\KB-list.txt') do (

echo "Uninstalling KB%%i"
wusa /uninstall /kb:%%i /quiet /norestart
)
echo "Uninstall(s) Complete."
echo.
echo "Done"
echo.
echo "Rebooting…"
echo.
shutdown /r

Thanks again!

1 comments

@LotPings 2018-10-11 14:04:33

Running systeminfo several times is quite inefficient.

Use findstr /g option to compare the whole list at once:

:: Q:\Test\2018\10\11\SO_52761793.cmd
@echo off
setlocal enabledelayedexpansion
set "Log=^>^> Logfile.log"
%Log% echo Hotfix and Updates list.
%Log% Echo(
for /f "tokens=2 delims=B" %%i in ('
    systeminfo^|findstr /g:KB-list.txt'
) do (
      echo [%date% %time%] Uninstalling KB%%i"
%Log% echo [%date% %time%] Uninstalling KB%%i"
    wusa /uninstall /kb:%%i /quiet /norestart && (echo [%date% %time%] Done)||(echo [%date% %time%] failed)
)

(   echo [!date! !time!] Uninstall(s^) Complete.
    echo(
    echo Done
    echo(
    echo Rebooting…
    echo(
) %Log%
shutdown /r

EDIT: stripped down variant without logging

:: Q:\Test\2018\10\11\SO_52761793.cmd
@echo off
setlocal enabledelayedexpansion
echo Hotfix and Updates list.
Echo(
for /f "tokens=2 delims=B" %%i in ('
    systeminfo^|findstr /g:KB-list.txt'
) do (
    echo Uninstalling KB%%i"
    wusa /uninstall /kb:%%i /quiet /norestart && (echo Done)||(echo failed)
)
echo Uninstall(s) Complete.
echo(
echo Done
echo(
echo Rebooting…
echo(
shutdown /r

@Killsek 2018-10-11 14:27:59

Thank you for the fast reply,I need it to show the progress of each KB and if its being uninstalled, how would i get it to do that with your script?

@LotPings 2018-10-11 14:36:54

See changes, I'm not sure if your file does contain the the prefix KB or pure numbers.

@Killsek 2018-10-11 14:42:13

In the kb-list.txt it is pure numbers (without the kb) but when I just tried the latest script it just flashes a black command window then closes, the other at-least show some information running then closed

@LotPings 2018-10-11 14:46:36

That's because ATM my for /f get's the prefix KB from sysinfo output. I change it to split at the B so %%i will have only the number

@Killsek 2018-10-11 14:53:40

Oh so how would i fix it? Would it be easier to just add KB in front of each number in he text file and eliminating the KB: in the script?

@LotPings 2018-10-11 14:57:06

The already changed delims=B should have fixed it.

@Killsek 2018-10-11 15:01:37

unfortunately i get the same results.

@LotPings 2018-10-11 15:10:40

Changed the echo ouput to log to a file, cause when rebooting the screen is no longer visible. Otherwise I tested it here succesfully.

@Killsek 2018-10-11 15:22:24

I ran the new script both with administrator and regular and it still just blacks out, i am not sure why its not working. I also copied it to a new pc and same thing happens.

@LotPings 2018-10-11 15:38:17

I ran this on my German locale, just checked if systeminfo output deviates in English - but no. How do you run the batch? Try from an open cmd window to be able to see error msgs.

@Killsek 2018-10-11 15:45:11

Ok, so I got it to work by manually going and opening a cmd prompt and then browsing to the directory. However, normally (since this will be used on a network path ) i just right click and open or run as administrator. Is it possible to not make the log file and dates and time but just to show thats its searching for the kb in the list and if found show its uninstalling that kb on the cmd window? also to make it run from double clock or runa as admin instead of manually doing the cmd window?

@Killsek 2018-10-11 15:56:08

ok the edited one works for the most part the only thing is it doesn't say searching and the kb number it just show uninstalling one if found. and once its done uninstalling it says it uninstalls are complete and then it says failed but it did unintall it.

@LotPings 2018-10-11 16:02:21

Well while testing I didn't actually uninstall - I uggest you temporarily remove the /quiet from the wusa command. The conditional execution on succes &&(...) and on fail ||(...) respond to exit code/errorlevel wusa sets on termination - this part isn't essential.

@Killsek 2018-10-11 16:09:11

ok, i will do that..how do you get it to show that its searching each kb in the list though instead of it just being blank unless it found a match to uninstall?

@LotPings 2018-10-11 16:40:54

Why should you iterate through hotfixes which are definitely NOT installed? To learn more about findstr and it's options read findstr /?or visit ss64.com/nt/findstr.html

@Killsek 2018-10-11 16:45:12

Its more a visual thing for people watching it process through each mainly...i was reading the link you gave actually hehe and thank you again !!!! Is there a way to fo it htough?

@LotPings 2018-10-11 16:53:00

Instead of directly processing the found KBs you could store them in variables (still running systeminfo only once) and then iterate the KB-list.txt and check if the current hotfix is set as a var. But this will be just a blink on the screen and IMO not worth the effort.

@Killsek 2018-10-11 17:09:26

I gotcha, thank you for everything your a life saver and I learned a lot; I appreciate the assistance.

Related Questions

Sponsored Content

15 Answered Questions

[SOLVED] How do I pass command line parameters to a batch file?

7 Answered Questions

[SOLVED] How to "comment-out" (add comment) in a batch/cmd?

15 Answered Questions

[SOLVED] Windows batch files: .bat vs .cmd?

3 Answered Questions

[SOLVED] How to check if a file exists from inside a batch file

5 Answered Questions

[SOLVED] Long commands split over multiple lines in Windows Vista batch (.bat) file

  • 2008-09-16 03:04:34
  • Dan
  • 326799 View
  • 595 Score
  • 5 Answer
  • Tags:   batch-file cmd

18 Answered Questions

[SOLVED] How can you echo a newline in batch files?

6 Answered Questions

[SOLVED] What is the current directory in a batch file?

  • 2010-12-12 00:54:59
  • Aaron de Windt
  • 425296 View
  • 372 Score
  • 6 Answer
  • Tags:   batch-file directory

23 Answered Questions

[SOLVED] Batch file to delete files older than N days

9 Answered Questions

[SOLVED] Create folder with batch but only if it doesn't already exist

1 Answered Questions

Sponsored Content