0

I am using this code in a batch file:

for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1

It looks like no matter what %user% you type in it will still set %i%+1 equaling 1... even if the %user% is not found.

If the %user% is not found, NET USER reports saying the user was not found, but because %i%=1 it continues on in the script.

How would I go about getting this to work? If the %user% is not found in /Domain then %i% needs to = 0

1 Answer 1

2
for /f %%f in ('"net user %user% /domain ^| findstr /i %group%"') do set /a i=%i%+1

you need to escape the pipe as it is with higher prio than for loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, the DO clause could be simplified to set /a i+=1
@npocmaka From what I see you only escaped the pipe. When I add the escape (carrot) it reports /I is an unknown option in NET. This error happens whether the user is good or not. Any other suggestions?
for /f %%f in ('net user %user% /domain ^| findstr /i "%group%"') do set /a i+=1 see double quoting
Or even 'net user %user% /domain 2^>NUL ^| findstr /i "%group%"' to suppress displaying error messages...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.