¿La tubería no funciona después de la consulta de registro?

i'm trying to check if there is a java installation on the machine with a batch file,and install java if necessary. However, the pipe doesnt work, and i get the reg query's result on the screen. Here is the code:

@ECHO OFF
cls

:checkjava
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" | find "ERROR" > NUL
if %errorlevel% == 0 
...

preguntado el 17 de mayo de 13 a las 08:05

3 Respuestas

reg query "HKLM\Software\JavaSoft\Java Runtime Environment" 2>&1 | find "ERROR" > NUL

the error message is printed in error stream so you need to redirect it in the &1. Here's more info : http://www.robvanderwoude.com/redirection.php

contestado el 17 de mayo de 13 a las 08:05

prueba esto:

reg query "HKLM\Software\JavaSoft\Java Runtime Environment" >nul 2>&1 && goto:OK || goto:fail

:OK
echo reg key found
pause
goto:eof

:fail
echo ERROR key not found!
pause
goto:eof

contestado el 17 de mayo de 13 a las 08:05

I tried this too, but there is something wrong, command shell closes immediately when executing this one.Any idea why? - feluna

It always goes to fail,whether i have java installed or not. - feluna

Error messages are sent to Standard Error and to Standard Output hence you need to redirect Standard Error to Standard Output before running find en la salida.

reg query "HKLM\Software\JavaSoft\Java Runtime Environment" 2>&1 | find "ERROR" > nul

Another thing is that reg query itself returns 0 or 1 on success or failure

Return Code: (Except for REG COMPARE)
  0 - Successful
  1 - Failed

So you may not have to use find en absoluto.

reg query "HKLM\Software\JavaSoft\Java Runtime Environment"  > nul 2> nul

if %errorlevel% == 0  goto success
echo "Not found"
goto end
:success
echo "Found"
:end

I am just printing Found/Not found - but you can take whatever action you want.

contestado el 17 de mayo de 13 a las 08:05

so how can i use those 0 or 1? sorry i m really new in this :) - feluna

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.