2

OS: Windows 7, Windows 8, Windows 8.1

Using the Windows command line, I can see the current user's logon time using the quser command. This results in more output than I desire, so I'm hoping to use for skip and tokens to get only the logon time and date.

As an example

c:\quser.exe
--------------------------
USERNAME         SESSIONNAME     ID    STATE      IDLE TIME      LOGON TIME
GenericUser      console         1     Active     1:22           03/11/2015 1:45 PM

Using for, I can whittle this down, but it's taking the space between the date and time as a delimiter and therefore only giving me the date:

c:\for /f "skip=1 tokens=6" %a in ('quser') do echo %a
--------------------------
03/11/2015

Note that the output is missing the "1:45 PM". Of note, I can't specify multiple tokens because there are only 6 headers, so I can't get a 7th token from the second line. How can I modify this to collect the time as well as the date?

*Don't worry about calculating the datediff, as I'll be handling that in VBScript once I can properly collect it.

Thanks in advance, Beems

1 Answer 1

6

First, you can get 7th token from second line:
for /f "skip=1 tokens=7" %a in ('quser') do @echo %a produces 1:45
but you may also use a * to get everything to the end of line:
for /f "skip=1 tokens=5*" %a in ('quser') do @echo %b produces: 03/11/2015 1:45 PM
which I think is what you're after.

From help for:

If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed.

2
  • Thanks, I tried using the token with an asterisk per a link I found, but it looks like my big problem was I did @echo %a instead of @echo %b. do you know how to explain that difference to me? Thanks! Beems. Commented Mar 25, 2015 at 21:09
  • 2
    @Beems for variables are implicitly created by cmd, starting from first you specify, so in this case %a refers to token 5 (which is last token before what interests you) and %b to rest of the line (minus any leading spaces) Commented Mar 25, 2015 at 21:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.