Skip navigation

Q. How can I set a variable to the current user's logon date and time?

If you don't need to know the logon time of user, even if they are logged off, but only need the logon date and time of the current logged on user, use LogonTime.bat.

The syntax for using LogonTime.bat is:

\[call\] LogonTime LT

Where LT is a call directed environment variable that will contain the date and time of the current user's logon, like MM/DD/YYYY HH:MM:SS.

NOTE: LogonTime.bat uses LogonSessions.exe, which should be located in a folder that is in your PATH, or in the same folder as LogonTime.bat.

LogonTime.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: LogonTime LT&goto :EOF
setlocal
set sess=N
call :quiet>nul 2>&1
if defined lt endlocal&set %1=%lt%&goto :EOF
endlocal&set %1=0
goto :EOF
:quiet
for /f "Tokens=*" %%a in ('logonsessions') do (
 set line=%%a
 call :parse
)
goto :EOF
:parse
set wrk=%line:Logon session=%
if "%wrk%" NEQ "%line%" set sess=Y&goto :EOF
set wrk=%line:User name:    =%
if "%wrk%" NEQ "%line%" set user=%wrk%&goto :EOF
set wrk=%line:Logon type:   =%
if "%wrk%" NEQ "%line%" goto isInt
set wrk=%line:Logon time:   =%
if "%wrk%" NEQ "%line%" goto lt
goto :EOF
:isInt
if "%wrk%" EQU "Interactive" goto :EOF
set sess=N
goto :EOF
:lt
if "%sess%" EQU "N" goto :EOF
set lt=%wrk%


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish