0

I have read several posts regarding my issue (e.g. this one) but still cannot get the following running:

for /f "tokens=*" %%U in ('query user * /server:MYSERVER') do (
    if %%U contains %USERNAME% then do something.
)

So the issue is that the string to be searched for is not hard coded but a variable. I have tried all variants of %% and !! but always failed. Is there the one variant I missed to try?

Edit:

This does not answer the actual question (so it remains open) but it solves the specific task in a different way. In case it´s useful for someone I post it here (rather than in a comment because there you cannot add line breaks).

for /F "tokens=1 delims=" %%U in ('query user * /server:MYSERVER') do (
    for /F "tokens=1 delims= " %%V in ("%%U") do (
        if /I "%%V" == "%USERNAME%" (
            echo You: %%U
        ) ELSE (
            echo      %%U
        )
    )
)

It is based on Mofi´s suggestion below. However, I don´t have any > characters in my output of the query command. For that reason I have used a simple space as delimiter. I only wonder why it works with token 1 because the output seems to start with a space, thus a delimiter (if it´s not #160 in reality).

The only bad thing on that is that it does not seem to be possible to add the 2>NUL to the command:

for /F "tokens=1 delims=" %%U in ('query user * /server:MYSERVER 2>NUL') do (
...

Then the batch file drops an error. That is a pity because this way you could prevent the (stderr) message if no user is logged on at all.

krza
  • 1
  • 1
  • I do not see any purpose in using a `for` loop, so perhaps what you're looking for is either, `%SystemRoot%\System32\quser.exe /Server:MyServer 2>NUL | %SystemRoot%\System32\find.exe /I "%UserName%" 1>NUL && (Do Something)`, or `%SystemRoot%\System32\query.exe User /Server:MyServer 2>NUL | %SystemRoot%\System32\find.exe /I "%UserName%" 1>NUL && (Do Something)` – Compo Jun 22 '21 at 18:23
  • Hi Mofi & Compo, thanks for the quick responses. • Reg. the user names: They contain letters and numbers only. But this is off-topic and works. The question is about the string comparison. • Reg. the for loop replacement: Have to try it out what exactly happens. Maybe it´s useful, thanks. • Reg. the purpose: Currently I just list the output of the command. But I´d like to "highlight" the lines which contain the own user name. It´s just one example, I had the same issue in other cases as well. • FYI: It would be easy with VBS or PS. But I can only use CMD in that specific case. – krza Jun 22 '21 at 19:01
  • It is still unclear for me what you really want, but it looks like you want to do something if the line assigned to loop variable `U` starts with the name of current user. For this case I suggest to use `for /F "delims=" %%U in ('%SystemRoot%\System32\query.exe user * /server:MYSERVER') do for /F "delims=> " %%V in ("%%U") do if /I "%%V" == "%USERNAME%" echo %%V is the current user.` to do a case-insensitive string comparison on equality of first space/right angle bracket delimited string of the line and run a single command or multiple commands in a command block if this condition is true. – Mofi Jun 23 '21 at 10:20
  • You could use use also `for /F "tokens=* delims=>" %%U in ('%SystemRoot%\System32\query.exe user * /server:MYSERVER') do echo %%U | %SystemRoot%\System32\findstr.exe /B /I /L /C:"%USERNAME%" >nul && echo Current user: %%U || echo %%U` which removes `>` from the beginning of the lines with user account data and outputs the line of current user with `Current user: ` at beginning. See also [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Jun 23 '21 at 10:32
  • @krza, is there a particular reason, why you've not attempted my previous methodoloy, instead of pointlessly using a `for` loop? You could do what your new code snippet shows, just like this: `%SystemRoot%\System32\quser.exe /Server:MYSERVER 2>NUL | %SystemRoot%\System32\findstr.exe "^[>]*%UserName%\>" 1>NUL && (Echo You: %UserName%) || Echo Not You!`, or even like this: `%SystemRoot%\System32\query.exe User /Server:MYSERVER 2>NUL | %SystemRoot%\System32\findstr.exe "^[>]*%UserName%\>" 1>NUL && (Echo You: %UserName%) || Echo Not You!` – Compo Jun 23 '21 at 17:50
  • Sorry, had not checked it yet, Compo, because the pipes take a bit more time for me ;) But I will definitively do it asap. However, the main question is still how to compare 2 vars within a for loop. My specific example is just a vehicle for that question although the example task itself is also important (and for that reason the solutions are very welcome as well). – krza Jun 23 '21 at 19:45
  • I'm a little confused at this stage, what your question now is. The first suggestion I'd make is to use `2>NUL` you need to escape the redirection character with a caret, `2^>NUL`. – Compo Jun 23 '21 at 21:15
  • This answers the question as it was stated: `for /f "tokens=*" %%U in ('query user * /server:MYSERVER') do (` line `set "loopVar=%%U"` line `if "!loopVar:%USERNAME%=!" neq "%%U" echo %%U contains %USERNAME%` (and it is the fastest way to solve this problem)... – Aacini Jun 24 '21 at 15:54

1 Answers1

0

As I can no longer determine what your question now is, all I can do is to provide some code which should do what your latest code snippet.

For /F "Skip=1 Tokens=*" %%U In ('%SystemRoot%\System32\query.exe User
 /Server:MYSERVER 2^>NUL') Do For /F %%V In ("%%U"
) Do For /F "Delims=>" %%W In ("%%V") Do If /I "%%W" == "%USERNAME%" (
    Echo You: %%U) Else Echo %%U
Compo
  • 36,585
  • 5
  • 27
  • 39