ComputersConsumer ElectronicsCell PhonesHome Theater & AudioGraphic Design & Video EditingInternetIndustrial Technology

A Simple Time Tracking Tool Using Batch Files and Windows Task Scheduler

Updated on March 5, 2017

Before We Begin

If you're a time conscious person like me, you have probably attempted to find out where your missing hours go. We often hit the "age of awakening", quarter-life crisis, or anything that strikes you and make you aware that you're not immortal and your time is running out. Before I begin, I have to set the line between being "conscious" and being "obsessed". The first one aims for self-improvement and the latter, I believe, is simply self-abuse. While being conscious about time keeps you productive, obsessing about it does the opposite. Having anxiety all my life, I am more than aware that stressing too much about something causes a person to procrastinate more.

Now that we have that cleared out..

Say hello to your nagging partner.
Say hello to your nagging partner.

Resources

Not as fancy as you thought? This is a simple tracker I made under 15 minutes and you can set it up in less than that amount of time. I'm a proud fan of resourcefulness and brief idea-into-code moments. I used tools that are pre-installed in every computer:

  • Windows Task Scheduler
  • Command Line
  • Notepad

If you're using a different operating system, you may use your OS's counterpart of these utilities.

Unlike your partners, these batch files don't ask for too much (disk space).
Unlike your partners, these batch files don't ask for too much (disk space).

Batch Files

I wrote three batch files that perform very specific tasks. These are all the files you need for the tracker. Each one contains a set of commands that are executed in the command line.

  • reasons.bat
    Collects the reasons you were away from your computer when you answer the question "Why were you away?"
  • timein.bat
    Logs the date and time that you logged in to your computer.
  • timeout.bat
    Logs the date and time that you logged off from your computer.

Simply open a notepad and paste the codes below and save them with a .bat file extension so you end up with three batch files.

Note: Different operating systems require different syntax. Despite having alternatives for the tools used, the codes below may only work for Windows and you will have to convert them for your operating system.

reasons.bat

echo off
set d=%date%
set t=%time%

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (C:\Users\<username>\Documents\Track\Logs\time-out.log) do (
set lasttimeout=%%a
)

echo You've been away from %lasttimeout% to %d% %t%.
set /p exp="Why were you away? "
echo From %lasttimeout% to %d% %t% - %exp%. >> C:\Users\<username>\Documents\Track\Logs\reasons.log
pause

timein.bat

echo %date% %time% >> C:\Users\<username>\Documents\Track\Logs\time-in.log

timeout.log

echo %date% %time% >> C:\Users\<username>\Documents\Track\Logs\time-out.log

Log Files

In order to track your time, your logged in and away time must be stored somewhere. Here we will use a simple .log file extension. Replace the directories indicated in the code. Make sure that the directories exist and keep all the log files in one directory for better organization. The log files automatically be generated if they don't already exist and will be updated for every time in and time out.

For reasons.bat, replace the following lines:

  • C:\Users\<username>\Documents\Track\Logs\time-out.log
  • C:\Users\<username>\Documents\Track\Logs\reasons.log

For timein.bat, replace the line below:

  • C:\Users\<username>\Documents\Track\Logs\time-in.log

For timeout.bat, replace the line below:

  • C:\Users\<username>\Documents\Track\Logs\time-out.log

You can run the batch files to see if it works by double-clicking each one. The log files should be automatically generated if they don't already exist and updated if they already do.

Scheduling The Execution

Now that the code is working, we have to schedule its execution. Our goal is for the timeout.bat program to run every time the computer locks; and the timein.bat and reasons.bat programs to run every time we log into or unlock the computer.

Launch the Task Scheduler application by typing the program name into the Search programs and files field after clicking the Windows Start icon or navigating to
Start > All Programs > Accessories > System Tools and clicking "Task Scheduler".

Create a task by clicking "Create Task..." from the Actions pane.

Next would be a series of photos of the Task Scheduler settings. We'll be creating two tasks - Time In and Time Out.

Launching the Windows Task Scheduler.
Launching the Windows Task Scheduler.
Task Scheduler: Create Task
Task Scheduler: Create Task
Task Scheduler: General Tab
Task Scheduler: General Tab
Task Scheduler: Triggers (On workstation unlock)
Task Scheduler: Triggers (On workstation unlock)

Triggers

We set the value for "Begin the task" to "On workstation unlock" and the "Specific user" to our username. This means that the timein.bat program will execute when we unlock or log in to the computer using the specified username.

Task Scheduler: Actions (Time In)
Task Scheduler: Actions (Time In)
Task Scheduler: Actions (Reasons)
Task Scheduler: Actions (Reasons)

Actions

This is the part where we indicate the location of the batch files or programs we want to execute on the triggers we set previously. To create a new action, click the "New..." button. We will create two actions for this task. The first one will run the timein.bat file to log the time stamp when you log in. The second one will run the reasons.bat file to ask for the reason why you were away.

Replace the paths below with the path where you saved your batch files:

Time In
Program/script: "C:\Users\<username>\Documents\Track\Batch Files\timein.bat"
Start in (optional): C:\Users\<username>\Documents\Track\Batch Files\

Reasons
Program/script: "C:\Users\<username>\Documents\Track\Batch Files\reasons.bat"
Start in (optional): C:\Users\<username>\Documents\Track\Batch Files\

Note: Do not remove the double quotation marks for the Program/script value. You may encounter errors for paths that have space in it (e.g. "Batch Files").

Task Scheduler: Conditions
Task Scheduler: Conditions
Task Scheduler: Settings
Task Scheduler: Settings

Conditions and Settings

For the Conditions tab, I unchecked "Start the task only if the computer is on AC power" under the Power section since I'm using a laptop and my charger is not always plugged in especially during meetings. I did not make any changes for the Settings tab.

The next screenshots will show you the settings for the Time Out task. You'll have to create a new task just as before. Some of the settings are similar to what we did for the Time In task so you can assume that if the screenshot is not present below, you can apply the same settings in the screenshots for the Time In task.

Task Scheduler: General Tab
Task Scheduler: General Tab
Task Scheduler: Triggers (On workstation lock)
Task Scheduler: Triggers (On workstation lock)
Task Scheduler: Actions (Time Out)
Task Scheduler: Actions (Time Out)

After completing the steps above, you should have two new tasks in the Task Scheduler Library. One for Time In and another one for Time Out.

Task Scheduler Library
Task Scheduler Library

When we first created the batch files, we ran it manually (double-click) to see if it works. Now that we have created the tasks on Windows Task Scheduler, we will try running the tasks manually from the Task Scheduler to see if it works. This type of testing is very important if you want to save time tracing where the problem was.

Follow the step below to run the task from the Task Scheduler.

Task Scheduler: Run
Task Scheduler: Run
Prompt
Prompt

After entering the reason, you may check the log files generated in the directory you put into the batch files.

Typing a reason.
Typing a reason.
Logs.
Logs.

End of Tutorial

You can now try locking and unlocking your computer to see if it works. In order to make this tool useful, you must get into the habit of locking your computer whenever you are away. It's good security practice after all so you're hitting two birds with one stone. You can also use the data you collected to assess your habits and change them for the better. This tool helped me a lot when submitting weekly task reports for weeks where there were too many meetings that did not require me to bring my laptop. I have not had the time to improve this tool. For now, it works just fine.

As you can see, I've been using this for quite some time now so I already have a few records. Disregard some of the weird data since I demonstrated this to a colleague before writing an article about it. He's been using it for a while now and he said he can't believe how much time his coffee breaks take. Sometimes, lunch lasts for more than an hour and before you know it, the day is about to end and you didn't get anything done. This tool helped us be more productive and check areas where we can improve.

But like I said before, don't restrict yourself too much. It's important to take regular breaks since working for a few straight hours can make you less productive and prone to making errors. Make changes where you can so you can increase your productivity without burning out.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.