Jump to content

[CMD/Batch] FOR - Delims, Skip & Tokens - Not Working As Intended


Recommended Posts

Hi all,

More batch script goodness. I'm using PSTools' psloggedon for this.

In CMD, if I input 'psloggedon -l -x \\HOSTNAME' I get the following result...

Connecting to Registry of \\HOSTNAME...
                                                                              
Users logged on locally:
         DOMAIN\USER_ONE
         DOMAIN\USER_TWO
         DOMAIN\USER_THREE
         DOMAIN\USER_FOUR

Using FOR in a batch script, I would expect skipping the first 2 populated lines (using /F) and setting the delimiter as '\' should allow me to pipe just the usernames to the screen, however it isn't working.

I am using the following...

@echo off
for /f "skip=2 tokens=2 delims=\" %%a in ('psloggedon -l -x \\%Hostname%') do set "LoggedOnUsers=%%a"
echo "%LoggedOnUsers%"

...which results in the variable %LoggedOnUsers% being echo'd as simply...

USER_TWO

The end result I am aiming for is to output a list of all usernames logged on to a Hostname, regardless of whether it's just 1 user or many users, and then offer a CHOICE to the user of which username they want to select.

For example, if a machine had 4 users logged on to it, the return would be;

A. USER_ONE

B. USER_TWO

C. USER_THREE

D. USER_FOUR

Select a user : A,B,C,D

[set variable based on whether input is A, B, C or D to the username for that selection]

Where am I going wrong?


Thank you.

*edit* Amended spelling.

Edited by haze1434
Link to post
Share on other sites

Let me rewrite your script in java, so you might see more clearly where the problem lies.

String user;
for (String username : scriptThatIsolatesUserNames()) {
    user = username;
}
System.out.println( user );

The clue is in the curly braces.

Link to post
Share on other sites

Ah, this seems to work...

@echo off
set "userone="
set "usertwo="
set "userthree="
set "userfour="
for /F "skip=1 tokens=2 delims=\" %%a in ('psloggedon -l -x \\%Hostname%') do if not defined userone set userone=%%a
for /F "skip=2 tokens=2 delims=\" %%b in ('psloggedon -l -x \\%Hostname%') do if not defined usertwo set usertwo=%%b
for /F "skip=3 tokens=2 delims=\" %%c in ('psloggedon -l -x \\%Hostname%') do if not defined userthree set userthree=%%c
for /F "skip=4 tokens=2 delims=\" %%d in ('psloggedon -l -x \\%Hostname%') do if not defined userthree set userthree=%%d
echo A. %userone%
echo B. %usertwo%
echo C. %userthree%
echo D. %userfour%

Does this seem OK? I don't want to be using 'bad code'.

Thank you.

Edited by haze1434
Link to post
Share on other sites

The point was that you were consistently overwriting the variable from within the loop, then checking the value of the variable outside of the loop. For will (should) do the bit after "do" (but on the same line) for each iteration.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...