0

I need a CMD script that is able to search through a text file based on a findstr command that has multiple strings in it to be searched for.


In this case, I have created a text file (kb.txt) that contains the result of the command:

wmic qfe list

The script I need will read the file and searches through it with the findstr command like:

findstr "kb3199321 kb3175631 kb3155567 kb3143345"

If the string is not found, it will output the string that is not found. The same goes for those that have been found. Example of output:

kb3199321 not found
kb3175631 found
kb3155567 found
kb3143345 not found

The script should loop through the findstr command for all its strings (not just the 4 listed above, it could be a few dozen to hundreds), and for each of them, echo if the string is found or not.


Note

If the findstr command is too limited (where it can't search through more than a hundred strings), then the script should read off all the strings from another separate text file (e.g. "searchfile.txt"), then search through the "kb.txt" to see if it is able to find the strings, and echo the same wanted results as above

phuclv
  • 37,963
  • 15
  • 156
  • 475
G. Walter
  • 13
  • 1
  • 3

1 Answers1

0

You can't search all the strings and display the results separately. You must search each string independently then check the return value to know if the string exists or not.

Another problem is that wmic prints its output in UTF-16, but findstr doesn't work with Unicode so you'll have to use find (which is enough for this situation).

You can put the strings to search in the search file, line-by-line, then use a for /F loop to go through the content of that file

@echo off

for /F %%f in (searchfile.txt) do (
    find /i "%%f" kb.txt >NUL
    if errorlevel 1 (
        echo %%f not found
    ) else (
        echo %%f found
    )
)

If you want to use findstr for more features then you'll need to convert wmic output to ANSI. For information on how to do that see

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • No need for such complexity. `errorlevel` is not changed by a `call` so there is no need for it to be passed to the subroutine as a parameter, hence no need to invoke `delayedexpansion`. The current errorlevel can be interpreted by the conventional `if errorlevel` syntax, so calling a subroutine is also overkill – Magoo Nov 29 '16 at 03:28
  • @Magoo thanks for that information, but as I've checked [`call` isn't on the list of [commands that doesn't set errorlevel](http://ss64.com/nt/errorlevel.html). I've moved the if into the loop and removed the subroutine as you suggested – phuclv Nov 29 '16 at 04:10
  • I believe that it may be that `call` is omitted to fend off the number of consequent "corrections" expecting `errorlevel` to have not changed in the main procedure having executed a subprocedure via a `call`. – Magoo Nov 29 '16 at 04:23
  • **You can't search all the strings and display the results separately. You must search each string independently then check the return value to know if the string exists or not.** Yes, I did just that. But when I had to go through and check for more than 3 dozen strings (or KBs), it became a really huge problem that I needed some sort of 'for' loop to do so. I did have an idea about doing it, but am an intro-level in scripting. This does solve the problem entirely, and also can be edited for other purposes. I hope others find it useful as I couldn't find a solution online. Many thanks! – G. Walter Nov 29 '16 at 05:16
  • if you `findstr` all the string at once then results for all those search string will be output in one place and you'll have to parse the output of that again, which is much more complex – phuclv Nov 29 '16 at 05:30