Page 1 of 1

Analysing error text in FOR statement

Posted: 24 Jun 2016 07:05
by sambul35
I'm trying to optimize a code section for speed, the snippet is a part of another FOR loop with %%G parameter, and queries Win Registry for presence of certain values:

Code: Select all

for /l %%N in (0,1,9) do (
   reg query "%key3%%%N"%valv3%" | findstr %%G 2>&1 && echo %%N || (find "ERROR:" && echo Break)
   echo !errorlevel! & pause
)

There're 2 error types printed to console:
a) "End of search: 0 match(es) found" (its printed when the analyzed value doesn't match %%G)
b) "ERROR: The system was unable to find the specified registry key or value" (it's printed when the %key3%%%N or value "%key3%%%N"%valv3%" doesn't exist)

The code needs to break out of the inner FOR loop, when error b) occurs and the key doesn't exist. I can do it in a multilevel FOR, but when simplifying the structure, I seems to need analyzing the error text rather than ERRORLEVEL, since both above errors give the same ERRORLEVEL 1 value. The above code can't find "ERROR:" in the error output, it just goes through all 9 loops. What's the right way to analyze the error text here?

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 07:47
by Squashman
We have a whole thread dedicated to breaking out of a FOR /L command. I will see if I can find it for you.

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 07:53
by Squashman

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 07:58
by sambul35
Thanks. It seems to be not a problem of breaking out (I can use Aacini's solution for that), but of identifying a condition WHEN to break out, i.e. analyzing the error TEXT in this case or a similar approach like assigning different errorlevels to the above 2 errors. Or may be its impossible in one loop level.

Code: Select all

set break=
for /l %%N in (0,1,9) do (
   if not defined break (
       reg query "%key3%%%N"%valv3%" | findstr %%G && echo %%N || (find "ERROR:" && set break=TRUE)
       echo !errorlevel! & pause)
)

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 08:53
by Compo
What is the actual command you are wanting to run within that for loop?
i.e. what are you trying to achieve

The command you are running is for example effectively:

Code: Select all

reg query "%key3%5"%valv3%" | findstr %%G 2>&1 && echo 5 || (find "ERROR:" && echo Break)
That has unbalanced doublequotes

If you are wanting to verify the value "%valv3%" against %%G in the registry key "%key3%5" then surely this command does that:

Code: Select all

Reg Query "%key3%5" /f "%%G" /v 1>Nul 2>&1&&(Echo 5)||(Echo Break)

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 09:34
by sambul35
That's a typo. The multilevel FOR version works well, but slow, so all I try to achieve is speed, and the below code is close to actual command. Further simplified the snippet for clarity, the problem is the same: I need to assign different errorlevels to both errors (reg query AND findstr) without complicating the snippet structure if achievable in one loop, otherwise either can give errorlevel 1:

Code: Select all

:: Several keys can match the query like Key1, Key2, Key3, but Keys4-9 may not exist. If the key exists, its valv3 can either be empty or not.
set break=
for /l %%N in (0,1,9) do (
   if not defined break (
       reg query "%key3%%%N"%valv3% | findstr /E "}" && echo %%N || echo Not found
       if !errorlevel! equ 1 set break=TRUE)
)

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 09:44
by aGerman
FOR /F, pipes, and the like can only process the stdout (stream 1). If you want to process the stderr (stream 2) you have to merge it with the stdout via 2>&1
Try

Code: Select all

for /f %%i in ('2^>^&1 reg query "HKCU\I don't exist" ') do echo %%i
for /f %%i in ('2^>^&1 reg query "HKCU\Control Panel\Desktop" /v "foo" /s') do echo %%i


Regards
aGerman

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 09:44
by Compo
sambul35 wrote:That's a typo.
<SNIP />
reg query "%key3%%%N"%valv3% <SNIP />

This is still not a valid reg query unless %valv3% ^<space>[/-]v<space>.*

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 09:56
by sambul35
@aGerman

Thanks for as always constructive feedback. Will try that. :wink:

Re: Analysing error text in FOR statement

Posted: 24 Jun 2016 10:02
by Compo
sambul35 wrote:@Compo
It depends how values are set. The code works great for 2 years in a multilevel version, but too many iterations hence slow.

Code: Select all

set "valv3= /v DevicePathName"

That's what I said!

Why don't you just give us more of the script, especially the outer for loop and the formation of the variables you're using.
There may well be a better way to achieve your goal.