|
Answer» I am trying to find out which users are assigned to a GROUP and from that move some files to a various folders.
Finding whether a user is part of a group is easy enough using
NET LOCALGROUP "GROUPNAME" | find /I /C "%username%"
It returns 1 if the user is part of the group and 0 if they are not. The problem is then further PROCESSING. I want to use the value 1 returned from above command. However setting a VARIABLE to the above line does not work and using %ERRORLEVEL% brings back 0 EVEN if the user is in the group which is not surprising as no error has occurred.
Any ideas to returning the value from NET LOCALGROUP "GROUPNAME" | find /I /C "%username%" and putting it into a variable. Which I then can use to process a few IF statements
sorry for posting this, usually I would struggle through but deadlines loom plus I am NEW to batch files.The standard way of running a command and parsing (processing) the output is provided by the FOR command with the /F switch. For documentation see many web sources or type FOR .? at the prompt. Example:
for /f %%A in ('net "GROUPNAME" ^| find /I /C "%username%"') do set value=%%A
Note: When executing a command in a FOR dataset, and parsing the output, if we wish to use a pipe, e.g. to FIND, a caret before the pipe symbol is necessary. It is an "escape character".
Or you can use the errorlevel.
NET LOCALGROUP "GROUPNAME" | find /I "%username%" >nul If not errorlevel 1 ( echo found "%username%" echo more commands here )
A problem with that method is that a user called 'Juliet' will also match a user called "Julie"
|