2

Is there anyway to do the below without delayed expansion (one line, broken for readability)?

%comspec% /v:on /c %windir%\System32\reg.exe import c:\temp\test.reg &
 if !errorlevel! neq 0 pause

If it were an echo, I know that call can be used, but doesn't seem to be available for use with the if.

user66001
  • 774
  • 1
  • 13
  • 36

2 Answers2

4

You could use conditional execution: reg.exe import c:\temp\test.reg || pause
|| means execute on failure (ie errorlevel neq 0).

You may may also want to try this example to see how it works:

find "this" && echo found || echo not found - it will inform you if 'this' was in input stream (entered from keyboard unless you redirect).

wmz
  • 3,645
  • 1
  • 14
  • 22
  • Hi wmz... Thanks for the answer, and the interesting Find example, I have tried `reg.exe import c:\temp\test.reg | find "ERROR:" && echo found || echo not found` however it always says `not found` when it is printed on screen right above it. Guessing piping is not what you mean by redirection?. Also apologies for accepting Andriy M's answer, but think that the errorlevel solution would be more suitable for my case. +1 though. – user66001 Jan 20 '13 at 03:27
  • @user66001 No worries :-) You do not need to use `find` you could use `||` directly as I (and Andriy M) shown. Anyway, your `find` is not working because `reg` sends error output to `stderr`, and pipe joins `stdout` with `stdin` . To get required result, you'd need to play with those streams a bit, try this: `2>&1 reg.exe import c:\temp\test.reg | find /i "ERROR:" >nul && echo found || echo not found` – wmz Jan 20 '13 at 18:47
  • This did work in cmd.exe directly, but couldn't get the lnk creation / properties box to accept the command, given it couldn't find a file called 2>&1 in the path. Any tricks to make this work in a shortcut, per change? – user66001 Jan 21 '13 at 00:00
  • @user66001 you could use `reg.exe import c:\temp\test.reg 2>&1 ...` You can put redirection either before or after commands (second being more popular, but I find first easier to read). I have not tried it though, if it's still a problem you always can put it in batch and reference batch in your link – wmz Jan 21 '13 at 10:01
  • I see, however I believe that the issue is one of nesting/parent cmd.exe, and would prefer to have a standalone solution. Anyone else know why `%comspec% /k %windir%\system32\reg.exe import c:\temp\test.reg 2>&1 | find /i "ERROR:" >nul && (echo found) || (echo not found) & pause` produces the found/not found, then pauses before returning you to a command prompt; but `%windir%\system32\reg.exe import c:\temp\test.reg 2>&1 | find /i "ERROR:" >nul && (echo found) || (echo not found) & pause` doesn't execute the pause (window blinks open) / causes other parts of the expression to fail? – user66001 Jan 21 '13 at 21:39
  • @user66001 Post it as new question please - comments should not be used for that. – wmz Jan 21 '13 at 21:44
  • I don't understand. My question relates to clarifying of the answer provided / issues with the solution addressing the question. How is this not inline with the purpose for comments? – user66001 Feb 01 '13 at 21:34
2

One way is to use this older syntax:

%comspec% ... & if errorlevel 1 pause

In this case, a special variant of if statement is used, one that tests the current errorlevel state.

Another option might be this:

%comspec% ... || pause

The || command delimiter is roughly equivalent to & if !errorlevel! neq 0, i.e. the subsequent command is executed only if the previous one terminated with a non-zero exit code.

Community
  • 1
  • 1
Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • Thanks Andriy M - Can you elaborate on the "The `||` command delimiter is __roughly__ equivalent to..."? – user66001 Jan 20 '13 at 03:28
  • I was referring to a piece of knowledge I had once gathered from [this answer](http://stackoverflow.com/questions/10354016/file-redirection-in-windows-and-errorlevel/10358437#10358437) (didn't have the link to it at the time). It may be irrelevant to your particular situation but, as I was making a general statement in my answer, I had to take various cases into account. I've edited the answer now to include the reference. – Andriy M Jan 21 '13 at 06:16
  • Ah, so `||` isn't using `errorlevel`, but for some reason causes the `errorlevel` to change after using it. P.S Why is dbenham referring to you in http://stackoverflow.com/questions/10354016/file-redirection-in-windows-and-errorlevel#comment13358780_10358437, when you don't seem to be mentioned in any other part of the Q&A prior? Don't think so, but I have a [question](http://meta.stackexchange.com/questions/163643/changed-handles-leading-to-confusion-in-comments) that may be related to this :) – user66001 Jan 21 '13 at 06:56
  • That was a reply to my, now deleted, comment. I think I was showing some "test case" there which seemed to me to be contradicting dbenham's answer, while in fact, I simply hadn't tested it thoroughly. – Andriy M Jan 21 '13 at 07:42