Analysing error text in FOR statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Analysing error text in FOR statement

#1 Post by sambul35 » 24 Jun 2016 07:05

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?
Last edited by sambul35 on 24 Jun 2016 08:50, edited 2 times in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Analysing error text in FOR statement

#2 Post by Squashman » 24 Jun 2016 07:47

We have a whole thread dedicated to breaking out of a FOR /L command. I will see if I can find it for you.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Analysing error text in FOR statement

#3 Post by Squashman » 24 Jun 2016 07:53


sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Analysing error text in FOR statement

#4 Post by sambul35 » 24 Jun 2016 07:58

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)
)

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Analysing error text in FOR statement

#5 Post by Compo » 24 Jun 2016 08:53

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)

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Analysing error text in FOR statement

#6 Post by sambul35 » 24 Jun 2016 09:34

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)
)
Last edited by sambul35 on 24 Jun 2016 09:46, edited 1 time in total.

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

Re: Analysing error text in FOR statement

#7 Post by aGerman » 24 Jun 2016 09:44

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Analysing error text in FOR statement

#8 Post by Compo » 24 Jun 2016 09:44

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>.*

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Analysing error text in FOR statement

#9 Post by sambul35 » 24 Jun 2016 09:56

@aGerman

Thanks for as always constructive feedback. Will try that. :wink:
Last edited by sambul35 on 24 Jun 2016 10:11, edited 2 times in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Analysing error text in FOR statement

#10 Post by Compo » 24 Jun 2016 10:02

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.

Post Reply