1

Is it possible, in one line, without a batch file to use findstr and only return a 0 %errorlevel% if all of the strings are found?

I am doing a gem list, which lists out a bunch of gems. I then want to use findstr/find to make sure that EACH string I search for exists. If one of them do not, then it should return a non 0 error code. Currently it will always return a 0 error code if any of the strings are found. It seems to work great with OR conditions, but not AND conditions. How can I do this?

C:... gem list | findstr "A B C D"

I only want a return code of 0 if it finds ALL of those. If D does not exist, it will still print out A B C, which gives the error code of 0.

Any ideas?

tek0011
  • 21
  • 4
  • What is the output of the "gem list" command? Is it one gem per line, or is it a single space-delimited line of gems? – Bill_Stewart Mar 09 '15 at 15:48

1 Answers1

1
FOR %G IN (foo, bar) DO (gem list | findstr %G)

Now if I can just get it to hold on to the error code for each return.

A better way to handle it and error if one of the commands in the loop fails:

FOR %G IN (foo, foobar) DO (gem list | findstr %G) || IF %ERRORLEVEL% == 0 exit

Then it will always return a non , giving you the correct errorlevel on exit

tek0011
  • 21
  • 4