Page 1 of 1

Time Stamping Batch File

Posted: 15 Jul 2010 14:18
by Landlord
Hi all,

Very new to programming and looking for help on a batch file.

What i need is a batch file to create a time stamp every time a user logs in, the mouse is moved, the keyboard is striked or the user logs off. This time stamp should be to the nearest second. dd/mm/yyyy hh:mm:ss format

Batch File will be named Time.bat
The batch file shall be located in C:\Bowe\Time.bat
The timestamps will be published in a notepad document called C:\Bowe\Results

from the notepad file I shall be taking it into an excel file to analysis so need it to the in a version that can be import to excel

Any help would be most appreciated. In a serious rush to get this completed.

Thanks in advance

Re: Time Stamping Batch File

Posted: 15 Jul 2010 14:39
by aGerman
This can not be done by Batch. Maybe you could log the "System Idle Process" run time (tasklist /v) but you can't write a key logger or mouse logger in batch. You have to learn a real programming language (like C/C++).

Regards
aGerman

Re: Time Stamping Batch File

Posted: 15 Jul 2010 14:58
by Landlord
Thanks for the reply aGerman.

If i was to log the 'system idle process' run time, and the log in and log off time that woudl probable give me the same data I am looking for.

The whole thing is trying to find when and for how long the computers in an office are not being used.

Do you know how I would write the system idle process time log and the log-off, log-on times?

Re: Time Stamping Batch File

Posted: 15 Jul 2010 16:03
by aGerman
Before I try to write a code, one last question:
Why do you want to run a batch file? IMO this is not a good idea. Each batch file has a window and it is very easy for the user to close it. Finally it's senseless if no log file is written.

Regards
aGerman

Re: Time Stamping Batch File

Posted: 15 Jul 2010 16:17
by Landlord
Was told by a 'programmer' that a batch file would be the best to write it in.
Guessing he is way off the mark?

Re: Time Stamping Batch File

Posted: 15 Jul 2010 16:54
by aGerman
Well, it's up to you to find out if he was wrong.
Try this code:
Time.bat

Code: Select all

@echo off &setlocal
if exist "tmp.log" >>"results.csv" type "tmp.log"
set "DateTime=%date:~-10% %time:~,8%"
for /f "delims=, tokens=8" %%a in ('tasklist /v /fo csv /nh^|findstr /i /c:"system idle process"') do set "SIPstart=%%~a"
:loop
for /f "delims=, tokens=8" %%a in ('tasklist /v /fo csv /nh^|findstr /i /c:"system idle process"') do set "SIPend=%%~a"
>"tmp.log" echo %username%,%DateTime%,%SIPstart%,%SIPend%
ping -n 3 localhost >nul
goto loop


Create a link into the "Startup" folder in "All Users" profile and set the focus minimized.

The code will overwrite tmp.log once in 2 seconds.
On each start of the batch code it will append the contents of tmp.log to results.csv.
C(omma)S(eparated)V(alues) files are running in Excel by default.
The columns are:
- user name
- date and time the batch file starts running
- System Idle Process run time the batch file starts running
- System Idle Process run time the batch file ends running
Of course, you have to calculate the values you need in Excel, but it's doable.

Hope that helps
aGerman

Re: Time Stamping Batch File

Posted: 15 Jul 2010 17:51
by ghostmachine4
Landlord wrote:What i need is a batch file to create a time stamp every time a user logs in, the mouse is moved, the keyboard is striked or the user logs off. This time stamp should be to the nearest second. dd/mm/yyyy hh:mm:ss format

the correct approach for your case is not to write your own batch, but to make use of the Operating System's auditing functions for the job. Search the internet for how to turn on various auditing functions for your system. all these will be logged to the Event logs. Then use a tool/vbscript to query the event logs to view them.

Re: Time Stamping Batch File

Posted: 15 Jul 2010 18:50
by aGerman
I agree with you. Using VBScript and WMI is the better way in this case.

Regards
aGerman

Re: Time Stamping Batch File

Posted: 16 Jul 2010 12:19
by avery_larry
If you have the ability to use a network program for monitoring --

spiceworks.com