Page 1 of 1

How-to hide error output from Reg Query command?

Posted: 27 May 2013 04:41
by Rubl7
Hi fellow batches,

I am trying to hide the error output from the Reg Query command, if a registry entry do not exist.

Normally I would just add the command "nul 2>&1" to hide any output the command will give. But if I add this to the first line that generates the error message in case the registry do not exist, then none of the registry entries which was initially found, will be transfered to the output file "del.txt", which is the main purpose of the command.

I would very much like to have a clean batch file, so I hope someone can help me with a way to hide only the error output.

You can see my command underneath:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Code Store Database\Distribution Units" /f "Java Runtime Environment" /s | find "HKEY_LOCAL_MACHINE" > del.txt
for /f "tokens=* delims= " %%a in (del.txt) do reg delete "%%a" /f > nul 2>&1

Regards
Rubl

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 05:22
by Endoro
your code works for me, eg. on the command line:

Code: Select all

for /f "tokens=* delims= " %a in (del.txt) do @reg delete "%a" /f > nul 2>&1
No message, no error message. Turn "echo on" to see, what happens.

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 05:55
by Rubl7
nono.. it is this line that causes the issue, if the mentioned registry path does not exist:

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Code Store Database\Distribution Units" /f "Java Runtime Environment" /s | find "HKEY_LOCAL_MACHINE" > del.txt

The next line is ok, as you also found out.

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 06:11
by Endoro
ok, try this:

Code: Select all

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Code Store Database\Distribution Units" /f "Java Runtime Environment" /s >nul 2>&1 | find "HKEY_LOCAL_MACHINE" > del.txt

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 06:50
by Rubl7
That does hide the error output. But it also does not forward the findings in the Reg Query and paste it into the "del.txt" file. Which means that the "del.txt" file will always be empty :(

Guess it just dumps the Reg Query findings in a black hole and then create an empty file.

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 06:51
by foxidrive
This seems to hide the STDERR which is all that's needed.

Code: Select all

reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Code Store Database\Distribution Units" /f "Java Runtime Environment" /s  2>nul | find "HKEY_LOCAL_MACHINE" > del.txt

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 07:29
by Endoro
yes, output must to "find", not to "nul" ... :oops:

Re: How-to hide error output from Reg Query command?

Posted: 27 May 2013 07:58
by Rubl7
Brilliant guys.. Thanks :D