I'm writing a batch script that returns logged in users, via the 'query user' command. However, I've encountered a difficulty that other threads here were unable to help me with. Here's my (almost) whole script, only missing some echoes at the end, for brevity:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET me=%~n0
SET parent=%~dp0
SET count=1
FOR /F "skip=1 tokens=* USEBACKQ" %%F IN (`query user`) DO (
::obtain line of output
SET var!count!=%%F
::obtain first character from line
CALL SET ^faf=^%%var!count!:~0,1%%
::testing purposes
CALL ECHO count:^!count! faf1:^!faf!
::remove the '>' character, if present
IF ^!faf! == ^> (
SET var!count!=!var%count%:~1! && ECHO success )
SET /a count=!count!+1
)
What it should do is get one of several lines of output, create a temporary variable "faf" that stores the first character from that line, which is then compared to the ">" character, which 'query user' always adds before the current username-if that character is detected, the variable is overwritten, omitting the first character. In the meantinme, the echo prints the current value of loop counter, and the temp variable.
The problem lies in the CALL SET command: it seemingly ignores the line containing the ">" character, for other lines it works as intended. Because of that, the IF never gets used, and echo prints the counter, but not the first character - for the ">" line.
Mind you, the lines are still stored in var1-6, and printing them out with "^" shows that even the line with ">" prints out fine. The only problem is detecting, and omitting, that damned angle bracket. Can anyone help me with this, please? What am I missing?