Employee clock in / clock out .bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Employee clock in / clock out .bat

#1 Post by Hemlok » 17 Nov 2019 15:56

I am working on a simple batch file to clock in & clock out, and keep track of activity. Also I have the ability to create user accounts, remove users, add and remove passwords. I cannot take full credit for this creation. I chopped it up from another unnamed poster online.

To the point.
I have it just where I want it. Except one feature. I would love for the ability to get employee totals in the area the says "view current times" in the admin.bat.
time.csv is created and stores this information. Im just not sure if it is possible. Any help would be greatly appreciated.

Thanks in advance, Hemlok

Below is code for both batch files. Just create two batch files. "admin.bat" & "Timeclock.bat".
Run Timeclock.bat and log in with user "admin", and create a password.

******

-admin.bat

Code: Select all

:::::::::::::::::::::::::::::::::::
:: Employee TIme Clock V 2.0      :
:: By Hemlok                      :
::                                :
::                                :
:::::::::::::::::::::::::::::::::::

@echo off
color 0F
mode con cols=45 lines=3

:admin
if exist pass.csv (goto login) else (goto create)

:login
set /p pass=Enter Password: 
findstr %pass% pass.csv > temp.txt
set /p comp=<temp.txt
goto next

:next
if %pass%==%comp% goto panel
goto login

:create
cls
set /p pass=Create Password: 
echo %pass% >> pass.csv
attrib +h pass.csv
goto panel

:panel
mode con cols=45 lines=10
del temp.txt
cls
echo 1. Set Up Employee
echo 2. Remove Employees
echo 3. View Employees
echo 4. Add Password
echo 5. Delete Passwords
echo 6. View Current Times
echo 7. Quit
echo.
set /p C=
if "%C%"=="1" goto employee
if "%C%"=="2" goto remove
if "%C%"=="3" goto viewcheck
if "%C%"=="4" goto newpass
if "%C%"=="5" goto delpass
if "%C%"=="6" goto timecheck
if "%C%"=="7" goto quit

:employee
cls
set /p emp=Enter Employee Name: 
set /p num=Enter Employee Number: 
findstr %num% Employees.csv >> temp.txt
set /p comp=<temp.txt
del temp.txt
if %num%==%comp% goto errors
goto employees

:employees
echo %emp% >> Employees.csv
echo %num% >> Employees.csv
echo. >> Employees.csv
attrib +h Employees.csv
echo Add another employee?
echo 1. Yes
echo 2. No
set /p add= 
if "%add%"=="1" goto employee
if "%add%"=="2" goto panel

:view
cls
type Employees.csv
echo.
pause
goto panel

:newpass
cls
set /p pass=Create Password: 
echo %pass% >> pass.csv
goto panel

:time
cls
type time.csv
pause
goto panel

:delpass
del /A:H "C:\Users\Admin\Desktop\Time Clock Bat\pass.csv"
goto create

:timecheck
if exist "C:\Users\Admin\Desktop\Time Clock Bat\time.csv" (goto time) else (goto error)

:viewcheck
if exist Employees.csv (goto view) else (goto error)

:error
cls
echo I am sorry it does not seem like that file exist yet.
pause
goto panel

:errors
cls
echo I am sorry that employee number is already in use. Please try another.
pause
goto employee

:remove
mode con cols=45 lines=5
set /p z=Enter Employee Name: 
cls
attrib -h -a Employees.csv
findstr /v %z% Employees.csv >> temp2.txt
del Employees.csv
findstr .* temp2.txt >> Employees.csv
del temp2.txt
attrib +h +a Employees.csv
echo Remove Another Employee?
echo 1. Yes
echo 2. No
set /p a=
if "%a%"=="1" goto remove
if "%a%"=="2" goto panel

:quit
exit




-Timeclock.bat

Code: Select all

:::::::::::::::::::::::::::::::::::
:: Employee TIme Clock V 2.0      :
:: By Hemlok                      :
::                                :
::                                :
:::::::::::::::::::::::::::::::::::

@echo off
color 0F
mode con cols=45 lines=25

Title Employee Time Clock
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yy=%DATE:~10,4%

::Time 
for /f %%a in ('time /t') do (
set standardtime=%%a
)

:name
cls
echo.
echo          **************************
echo          *[----------------------]*
echo          *[ Employee Time Clock  ]*
echo          *[        V 2.0         ]*
echo          *[                      ]*
echo          *[     %mm%-%dd%-%yy%       ]*
echo          *[                      ]*
echo          *[                      ]*
echo          *[       %standardtime%          ]*
echo          *[                      ]*
echo          *[                      ]*
echo          *[----------------------]*
echo          **************************
echo.
set /p name=Enter Your Name: 
if %name%==admin goto admin
set /p input=Enter Employee Number: 
findstr %input% Employees.csv > temp1.txt
findstr %name% Employees.csv > temp2.txt
goto next

:next
set /p comp=<temp1.txt
if %input%==%comp% goto then
goto error

:then
set /p com=<temp2.txt
if %name%==%com% goto menu
goto error

:menu
del temp2.txt
del temp1.txt
cls
echo.
echo                   `/ooo/`         
echo                  .do-.-sh`        
echo                  :m`   -m-        
echo                 `oy+++++y+`       
echo                 -mmmy+ymmm-       
echo                 -mmms smmm-       
echo                 -mmmm/mmmm-       
echo                 `sssssssss`
echo.
echo                  Time %standardtime%
echo             ********************
echo             *[                ]*
echo             *[ 1. Clock In    ]*
echo             *[                ]*
echo             *[ 2. Clock out   ]*
echo             *[                ]*
echo             *[ 3. Exit        ]*
echo             *[                ]*
echo             ********************
set /p c= 
if "%c%"=="1" goto in
if "%c%"=="2" goto out
if "%c%"=="3" exit

:in
echo %name%,%input%,IN,%standardtime%,%date% >> "C:\Users\Admin\Desktop\Time Clock Bat\time.csv"
cls
goto thankyou

:out
echo %name%,%input%,OUT,%standardtime%,%date% >> "C:\Users\Admin\Desktop\Time Clock Bat\time.csv"
cls
goto thankyou

:admin
call admin.bat

:error
echo Your input does not match our records please try again
echo.
pause
goto name

:thankyou
echo Time Accepted!
pause
:end
Last edited by aGerman on 17 Nov 2019 18:22, edited 1 time in total.
Reason: code formatting

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Employee clock in / clock out .bat

#2 Post by Hemlok » 17 Nov 2019 19:00

One more detail.

You will need to edit this part of the admin.bat to your own folder path where Timeclock lives.

Code: Select all

:delpass
del /A:H "C:\Users\Admin\Desktop\Time Clock Bat\pass.csv"
goto create

:timecheck
if exist "C:\Users\Admin\Desktop\Time Clock Bat\time.csv" (goto time) else (goto error)

And this part of Timeclock.bat to your own folder path where Timeclock lives.

Code: Select all

:in
echo %name%,%input%,IN,%standardtime%,%date% >> "C:\Users\Admin\Desktop\Time Clock Bat\time.csv"
cls
goto thankyou

:out
echo %name%,%input%,OUT,%standardtime%,%date% >> "C:\Users\Admin\Desktop\Time Clock Bat\time.csv"
cls

Also when you first log in- create a new employee right off the rip, to create the files needed in your Timeclock directory (time.csv, and the other hidden files.). The messages you get when adding the first user does not happen after having the files created. It appears to work flawless after first user add. It just lets you know the files did not exist initially.

I hope this makes sense..

Again. I am only needing the ability to bring up user time totals from the admin menu. Somehow calculate what is in time.csv and display the results from a choice in the menu. Everything else is working great as far as I can tell.

-Hemlok

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

Re: Employee clock in / clock out .bat

#3 Post by aGerman » 18 Nov 2019 10:57

I think you forgot to mention that one has to create the "Time Clock Bat" folder, too ...
It would have been much easier to copy the text you find in time.csv instead of leaving us alone with scripts that create hidden files and stuff which is not necessary to answer your question.
It's of much more interest what format time /t and echo %time% output on your computer since both formats are locale-dependent and will be certainly completely different from what I see in my csv file.

Steffen

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Employee clock in / clock out .bat

#4 Post by Hemlok » 18 Nov 2019 11:32

Your right. Please forgive my lapse in memory. In retrospect I should have thought out my post a bit more.

My format is as follows:
Time: 11:30 AM
The date: 11/18/2019.
I am in Central Standard time if that matters.

I do appreciate your time.

-Hemlok

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

Re: Employee clock in / clock out .bat

#5 Post by aGerman » 18 Nov 2019 12:20

I figured I would see something like AM and PM. You have to convert your 12 hours to 24 hours time and split the date and time into their separate values.
Batch doesn't support calculations of date and time values. Thus you have to convert them into their representation in seconds first, and then you can calculate the difference between two values in seconds.
You may want to download Ritchie Lawrence's function library:
https://ritchielawrence.github.io/batchfunctionlibrary/
The DateToSecs function should help to achieve your task. Not that trivial though because beforehand you have to find the pairs of IN and OUT values in your csv file that belong together.

Steffen

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Employee clock in / clock out .bat

#6 Post by Hemlok » 18 Nov 2019 12:38

Thanks. I figured. I have a version I created with a 24 hr format. I got lost after that. To be honest, you left me in the dust on the rest.

Thanks for your time.

-Hemlok

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

Re: Employee clock in / clock out .bat

#7 Post by aGerman » 18 Nov 2019 13:45

You can get a consistent datetime format using WMIC if you don't insist on having it in a localized format in your csv file.
From a few days ago:
viewtopic.php?f=3&t=9348#p60703

Steffen

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Employee clock in / clock out .bat

#8 Post by Hemlok » 18 Nov 2019 15:15

I'm not having a formatting issue. I'm not sure how to calculate from the .csv and call on the results.
I can format it in any way I want without issue. Im completely aware of how to accomplish that.

Never mind. I will figure it out. We are not communicating here.

Thank you.

-Hemlok

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

Re: Employee clock in / clock out .bat

#9 Post by aGerman » 18 Nov 2019 16:55

Okay that's from where you can start.

Code: Select all

@echo off &setlocal

:: get the current time values
call :GetDateTimeValues year1 month1 day1 hour1 minute1 second1
echo %year1%-%month1%-%day1% %hour1%:%minute1%:%second1%

:: delay for testing
timeout /t 5

:: again get the current time values
call :GetDateTimeValues year2 month2 day2 hour2 minute2 second2
echo %year2%-%month2%-%day2% %hour2%:%minute2%:%second2%

echo(

:: get the seconds elapsed since 1st January 1970 00:00:00 for each datetime
call :DateToSecs %year1% %month1% %day1% %hour1% %minute1% %second1% secs1
call :DateToSecs %year2% %month2% %day2% %hour2% %minute2% %second2% secs2

:: calculate the differences
set /a "diff=secs2-secs1"
echo %diff% seconds elapsed
pause
exit /b


:GetDateTimeValues year month day hour minute second
setlocal
for /f %%i in ('WMIC OS GET LocalDateTime /value') do for /f %%j in ("%%i") do set "%%j"
(
  endlocal
  set "%~1=%LocalDateTime:~0,4%"
  set "%~2=%LocalDateTime:~4,2%"
  set "%~3=%LocalDateTime:~6,2%"
  set "%~4=%LocalDateTime:~8,2%"
  set "%~5=%LocalDateTime:~10,2%"
  set "%~6=%LocalDateTime:~12,2%"
)
goto :eof


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
::
:: By:   Ritchie Lawrence, updated 2002-08-13. Version 1.1
::
:: Func: Returns number of seconds elapsed since 1st January 1970 00:00:00
::       for a given calendar date and time of day. For NT4/2000/XP/2003.
:: 
:: Args: %1 year to convert, 2 or 4 digit (by val)
::       %2 month to convert, 1/01 to 12, leading zero ok (by val)
::       %3 day of month to convert, 1/01 to 31, leading zero ok (by val)
::       %4 hours to convert, 1/01 to 12 for 12hr times (minutes must be
::          suffixed by 'a' or 'p', 0/00 to 23 for 24hr clock (by val)
::       %5 mins to convert, 00-59 only, suffixed by a/p if 12hr (by val)
::       %6 secs to convert, 0-59 or 00-59 (by val)
::       %7 var to receive number of elapsed seconds (by ref)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
if 1%hh% LSS 20 set hh=0%hh%
if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a j=j*86400+hh*3600+nn*60+ss
endlocal&set %7=%j%&goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Still you have to figure out how to read the values. Some thoughts:

Initialize a variable for the total number of seconds with 0.
Use findstr to filter the filecontent. E.g. lines beginning with a certain name and id.
Process the output of findstr in a for /f loop where you use the comma as delimiter.
Save the date and time value for the line where you found IN in the 3rd column.
If you read OUT in the 3rd column, use the saved values and the currently read values to calculate the difference like shown in the code above.
Add the calculated difference to the variable for the total number of seconds.
If you computed all values of a certain employee, resume with the next.

Steffen

Hemlok
Posts: 10
Joined: 17 Nov 2019 15:32

Re: Employee clock in / clock out .bat

#10 Post by Hemlok » 18 Nov 2019 17:04

Fantastic. Im on it. Thanks for the ideas. Exactly what I was looking for.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Employee clock in / clock out .bat

#11 Post by Aacini » 20 Nov 2019 00:05

Perhaps this answer may help you...

Antonio

Post Reply