0

I am expecting to receive just an output "NO User exists for *" after running this code:

FOR /F "tokens=* delims= " %%A IN ('C:\windows\System32\query.exe user /server:<some_IP_address>') DO SET NumDoc1=%%A
echo %NumDoc1%

But I keep getting:

No User exists for * 
ECHO is off.

How do I get rid of "ECHO is off" from my output?

Thanks

JerryH
  • 3
  • 2

2 Answers2

4

When no users are found, the output is being sent to Standard Error. The FOR command is just capturing Standard Output. So you need to redirect Standard Error to Standard output.

for /f "delims=" %%G IN ('"query user /server:servername 2>&1"') do set NumDoc1=%%G
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • @JerryH, please read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Squashman Nov 12 '18 at 18:04
3

You are echoing the current echo status because your variable is empty.

Try this instead:

echo NumDoc1:%NumDoc1%

pcgben
  • 726
  • 7
  • 24
  • I tried your way, and it just displays this now: No User exists for * NumDoc1: – JerryH Nov 09 '18 at 00:06
  • That is the expected outcome. This is because NumDoc1 contains no value. I understand the `ECHO is off.` status message no longer appears? – pcgben Nov 09 '18 at 00:16