Jared
Jared

Reputation: 1897

Redirecting windows system errors

I have the equivalent of the following in a batch script:

net localgroup "Cool Usergroup"

This may succeed, or it may fail for various reasons including:

System error 1376 has occurred.

The specified local group does not exist.

or

System error 5 has occurred.

Access is denied.

The problem is I need to look for these error codes, but the output is not coming through standard out or standard error. I've tried this:

net localgroup "Cool Usergroup" 2>&1 > %temp%\myGarbage.txt

But I still get the output splashed on the screen, not in the file.

To give a little more context, my original command is running in a for loop, and I'd like to get it working inside there, but there are additional problems with that.

for /f "tokens=*" %i in ( 'net localgroup "Cool Usergroup" 2>&1') do echo out: %i

gives me:

2>&1 was unexpected at this time.

So, how do I process these system errors in a batch script using a for loop? It doesn't seem the output goes through standard error or standard out.

Upvotes: 0

Views: 2087

Answers (2)

Jared
Jared

Reputation: 1897

What I was looking for just required some extra character escapes:

for /f "tokens=*" %i in ('net localgroup ^"Cool Usergroup^" 2^>^&1') do echo out: %i

That way I can parse for specific error messages in the loop. Keeping the accepted answer with Kamyar though since he gave me the breakthrough I needed.

Upvotes: 1

Kamyar Souri
Kamyar Souri

Reputation: 1923

Try this, it works on my system:

net localgroup "Cool Usergroup" > %temp%\myGarbage.txt 2>&1

Upvotes: 1

Related Questions