Time Stamping Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Landlord
Posts: 3
Joined: 15 Jul 2010 14:05

Time Stamping Batch File

#1 Post by Landlord » 15 Jul 2010 14:18

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Time Stamping Batch File

#2 Post by aGerman » 15 Jul 2010 14:39

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

Landlord
Posts: 3
Joined: 15 Jul 2010 14:05

Re: Time Stamping Batch File

#3 Post by Landlord » 15 Jul 2010 14:58

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Time Stamping Batch File

#4 Post by aGerman » 15 Jul 2010 16:03

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

Landlord
Posts: 3
Joined: 15 Jul 2010 14:05

Re: Time Stamping Batch File

#5 Post by Landlord » 15 Jul 2010 16:17

Was told by a 'programmer' that a batch file would be the best to write it in.
Guessing he is way off the mark?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Time Stamping Batch File

#6 Post by aGerman » 15 Jul 2010 16:54

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Time Stamping Batch File

#7 Post by ghostmachine4 » 15 Jul 2010 17:51

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Time Stamping Batch File

#8 Post by aGerman » 15 Jul 2010 18:50

I agree with you. Using VBScript and WMI is the better way in this case.

Regards
aGerman

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Time Stamping Batch File

#9 Post by avery_larry » 16 Jul 2010 12:19

If you have the ability to use a network program for monitoring --

spiceworks.com

Post Reply