How-to hide error output from Reg Query command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rubl7
Posts: 5
Joined: 10 Mar 2013 06:16

How-to hide error output from Reg Query command?

#1 Post by Rubl7 » 27 May 2013 04:41

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#2 Post by Endoro » 27 May 2013 05:22

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.

Rubl7
Posts: 5
Joined: 10 Mar 2013 06:16

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

#3 Post by Rubl7 » 27 May 2013 05:55

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.
Last edited by Rubl7 on 27 May 2013 06:51, edited 1 time in total.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#4 Post by Endoro » 27 May 2013 06:11

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

Rubl7
Posts: 5
Joined: 10 Mar 2013 06:16

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

#5 Post by Rubl7 » 27 May 2013 06:50

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#6 Post by foxidrive » 27 May 2013 06:51

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

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

#7 Post by Endoro » 27 May 2013 07:29

yes, output must to "find", not to "nul" ... :oops:

Rubl7
Posts: 5
Joined: 10 Mar 2013 06:16

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

#8 Post by Rubl7 » 27 May 2013 07:58

Brilliant guys.. Thanks :D

Post Reply