Need Help With Batch Script

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Need Help With Batch Script

Post by jadecook on Tue Aug 21, 2012 3:59 pm
([msg=68886]see Need Help With Batch Script[/msg])

Alright, I've been trying to code this batch script for the past couple days but I'm sort of stuck. What I want to do is make it where when I am at work I can "log" into this script, it records the time I "logged in", and at the end of the week it spits out how many hours I worked, along with all of the log in/out times and dates. This is what I have so far:

Code: Select all
@echo OFF
color a0
title Time Card
set start=1
cls
GOTO start

:start
if "%start%"=="2" (GOTO add) else GOTO first

:first
set /p hour=ENTER HOUR:
if "%hour%"=="1" GOTO one
if "%hour%"=="2" GOTO two
if "%hour%"=="3" GOTO three
if "%hour%"=="4" GOTO four
if "%hour%"=="5" GOTO five
if "%hour%"=="6" GOTO six
else GOTO addhours

:: Convert to Military Time

:one
set hour=13
GOTO addhours
:two
set hour=14
GOTO addhours
:three
set hour=15
GOTO addhours
:four
set hour=16
GOTO addhours
:five
set hour=17
GOTO addhours
:six
set hour=18
GOTO addhours

:: Convert hours and get minutes
:addhours
set /a hoursmin= %hour% * 60
set /p minutes=ENTER MINUTE:
set /a total1= %hoursmin% + %minutes%
set /a start=%start% + 1
GOTO start


::Second Time Around

:add
set /p hour=ENTER HOUR:
if "%hour%"=="1" GOTO one1
if "%hour%"=="2" GOTO two2
if "%hour%"=="3" GOTO three3
if "%hour%"=="4" GOTO four4
if "%hour%"=="5" GOTO five5
if "%hour%"=="6" GOTO six6
else GOTO addhours

:one1
set hour=13
GOTO addhours1
:two2
set hour=14
GOTO addhours1
:three3
set hour=15
GOTO addhours1
:four4
set hour=16
GOTO addhours1
:five5
set hour=17
GOTO addhours1
:six6
set hour=18
GOTO addhours1

:: Convert hours and get minutes
:addhours1
set /a hoursmin= %hour% * 60
set /p minutes=ENTER MINUTE:
set /a total2= %hoursmin% + %minutes%
set /a total1=%total1% + %total2%
echo YOU'VE WORKED %total1% MINUTES
GOTO start



If someone could help me figure this out I would be so grateful!
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 75
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by WallShadow on Tue Aug 21, 2012 4:24 pm
([msg=68887]see Re: Need Help With Batch Script[/msg])

You simply made 1 tiny mistake, instead of subtracting start time from end time, you added both together causing the logical error. change this set of lines:


Code: Select all
    :: Convert hours and get minutes
    :addhours1
    set /a hoursmin= %hour% * 60
    set /p minutes=ENTER MINUTE:
    set /a total2= %hoursmin% + %minutes%
    set /a total1=%total2% - %total1%
    echo YOU'VE WORKED %total1% MINUTES
    GOTO start


set /a total1=%total2% - %total1% is really the only line that I changed, and now it works like a charm.

Personally, if you want to make this kind of a script, I recommend using the TIME command or using the %TIME% environment variable, though you'd need some fancy string manipulation for that.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by jadecook on Wed Aug 22, 2012 7:13 am
([msg=68912]see Re: Need Help With Batch Script[/msg])

Would you know that "fancy string manipulation"? Or do you have any ideas that would make this script more convenient and store more data?
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 75
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by WallShadow on Wed Aug 22, 2012 10:58 am
([msg=68923]see Re: Need Help With Batch Script[/msg])

This is my take on this task:

Code: Select all
@ECHO OFF
SETLOCAL EnableDelayedExpansion




ECHO(recording current time

CALL :GetCurrentTime -m
SET _T_total=!_R_return1!

ECHO(
ECHO(
ECHO(press enter when you want to stop recording time ...
PAUSE>nul






CALL :GetCurrentTime -m
SET /A _T_total = !_R_return1! - !_T_total!

ECHO(total minutes worked: !_T_total!



SET /A _T_totalhours = !_T_total! / 60
SET /A _T_totalminutes = !_T_total! - _T_totalhours * 60

ECHO(you have worked for a total of !_T_totalhours! hours and !_T_totalminutes! minutes





GOTO :END

















:: GetCurrentMinutes subroutine

:: if no parameters are specified;
:: return1 is set to the current number of hours from the last 0-00
:: return2 is set to the current number of minutes from the last hour

:: if the first parameter is set to '-m' ;
:: return1 is set to the total number of minutes from the last 0-00


:GetCurrentTime
SETLOCAL EnableDelayedExpansion

SET _V_TimeString=!TIME!

SET /A _V_Hours = !TIME:~0,2!
SET /A _V_Minutes = !TIME:~3,2!


if "%1"=="-m" (
   
   SET /A _V_Minutes += !_V_Hours! * 60
   
   SET _R_returns=^
      SET _R_return1=!_V_Minutes!
   
) ELSE (
   
   SET _R_returns=^
      SET _R_return1=!_V_Hours!^&^
      SET _R_return2=!_V_Minutes!
   
)

:: I have no idea how to solve this attack vector ...

ENDLOCAL & %_R_returns%
GOTO :eof









:END

ECHO(
ECHO(
ECHO(press any key to close the program ...
PAUSE>nul

ENDLOCAL

GOTO :eof


Simple program, start it, leave it running in the background, then when you're finished with work, press enter and it automatically outputs the total minutes you've worked and the combined hours and minutes you've worked. feel free to copy pasta and learn off of it.

I was actually wanting to program this for a while, so thanks!

Btw, cookie to the first person to be able to use command injection on this thing, though I doubt it, I've made it mostly injection proof. the only real injection you can do is changing the time variable to some crazy shit and hope that you can inject something with the 2 sets of 2 characters that the code does use unsafely. I wanted to make it also record AM/PM which can be done with the TIME command but I'm to lazy for that today, I apologize.

-WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by jadecook on Wed Aug 22, 2012 1:26 pm
([msg=68927]see Re: Need Help With Batch Script[/msg])

WallShadow wrote:This is my take on this task:

Code: Select all
@ECHO OFF
SETLOCAL EnableDelayedExpansion




ECHO(recording current time

CALL :GetCurrentTime -m
SET _T_total=!_R_return1!

ECHO(
ECHO(
ECHO(press enter when you want to stop recording time ...
PAUSE>nul






CALL :GetCurrentTime -m
SET /A _T_total = !_R_return1! - !_T_total!

ECHO(total minutes worked: !_T_total!



SET /A _T_totalhours = !_T_total! / 60
SET /A _T_totalminutes = !_T_total! - _T_totalhours * 60

ECHO(you have worked for a total of !_T_totalhours! hours and !_T_totalminutes! minutes





GOTO :END

















:: GetCurrentMinutes subroutine

:: if no parameters are specified;
:: return1 is set to the current number of hours from the last 0-00
:: return2 is set to the current number of minutes from the last hour

:: if the first parameter is set to '-m' ;
:: return1 is set to the total number of minutes from the last 0-00


:GetCurrentTime
SETLOCAL EnableDelayedExpansion

SET _V_TimeString=!TIME!

SET /A _V_Hours = !TIME:~0,2!
SET /A _V_Minutes = !TIME:~3,2!


if "%1"=="-m" (
   
   SET /A _V_Minutes += !_V_Hours! * 60
   
   SET _R_returns=^
      SET _R_return1=!_V_Minutes!
   
) ELSE (
   
   SET _R_returns=^
      SET _R_return1=!_V_Hours!^&^
      SET _R_return2=!_V_Minutes!
   
)

:: I have no idea how to solve this attack vector ...

ENDLOCAL & %_R_returns%
GOTO :eof









:END

ECHO(
ECHO(
ECHO(press any key to close the program ...
PAUSE>nul

ENDLOCAL

GOTO :eof


Simple program, start it, leave it running in the background, then when you're finished with work, press enter and it automatically outputs the total minutes you've worked and the combined hours and minutes you've worked. feel free to copy pasta and learn off of it.

I was actually wanting to program this for a while, so thanks!

Btw, cookie to the first person to be able to use command injection on this thing, though I doubt it, I've made it mostly injection proof. the only real injection you can do is changing the time variable to some crazy shit and hope that you can inject something with the 2 sets of 2 characters that the code does use unsafely. I wanted to make it also record AM/PM which can be done with the TIME command but I'm to lazy for that today, I apologize.

-WallShadow <3


This is awesome! :D

But... I don't mean to keep asking you stuff. I want everyone in the office to run this program. And I don't think that everyone will keep it going everyday they are at work. Is there anyway for a log in, log out? Like right now for example my time card reads:

IN: 8:02
OUT: 12:37
IN: 1:06
OUT: 4:44

And it does that for the whole week. Is there anyway to write a script to have a log in that records the date/time to a text file, then at the end of the week you can type print or something and it compiles at text document with all clock in and clock outs along with total hours worked?

So the text output will look something like this:

%NAME% %DATE%

%DATE% IN: %TIME%
%DATE% OUT: %TIME%
%DATE% IN: %TIME%
%DATE% OUT %TIME%

TOTAL HOURS WORKED THIS WEEK: %HOURS%
TOTAL HOURS WORKED THIS YTD: %HOURS%
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 75
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by WallShadow on Wed Aug 22, 2012 2:10 pm
([msg=68930]see Re: Need Help With Batch Script[/msg])

Yes, it is simple to create a batch that logs that and runs automatically when to user logs on or off. There are special keys in the registry that trigger programs to run when certain things happen (e.i. System turns on, user logs on, user logs off). How do you think some malware automatically turns on when a computer is started and immediately starts harassing the user?

What you're asking is starting to get a tad complex as I don't want a program that runs the entire duration of the user's time on the computer because 1, he might just close the window and thus killing the script, 2, that's just inefficient. It's also simple to just link the program to run when it the user logs on and when he logs off, but it gets more complex in terms of calculating hours. Don't get me wrong, it's possible and I've done it before, but its difficult because it requires that the script parse the previous log on record to calculate the distance between log on and log off.

I'll try my hand on it in a few hours when I have the time to do so. For now it would be nice if you can post the computer OS to make sure that the registry keys I'll use will work. Another note is that I can't get the name of the person using it. There is a command called WHOAMI which returns the group and username of the currently logged on user which works well if the usernames are based on people's names, but if that username is something like LiquidGenesis48, you'll have a tough time figuring out who in the world it is that logged in.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by jadecook on Wed Aug 22, 2012 2:24 pm
([msg=68932]see Re: Need Help With Batch Script[/msg])

Makes sense, and you're awesome! I've been trying to get this working for awhile now. With the "user name" I don't really mean user name, I mean their real name. Maybe even have a script like:

Code: Select all
if %hours%=="new" GOTO new

:new
set /a first=FIRST NAME:
set /a last=LAST NAME:
echo %first% %last% >> filetosaveuserandcalllater.r


If ya know what I mean?

PS. The OS is Windows. I think some people are XP but most are 7.
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 75
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by LoGiCaL__ on Wed Aug 22, 2012 8:01 pm
([msg=68939]see Re: Need Help With Batch Script[/msg])

There's actually a log with this info already recorded for you. You can check logins and logouts from the event viewer -> security log. Just look for the user name. That is assuming that each user has a unique name on the PC. Anyway you decide to do it, I think you would be better off with powershell or vbscript since you're on windows.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1060
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by tremor77 on Thu Aug 23, 2012 1:00 am
([msg=68946]see Re: Need Help With Batch Script[/msg])

If this is designed as a pseudo time clock software, what you really need to do is run a monitoring script that logs for periods of inactivity. Whats to stop someone from punching the login script.. sitting around all day or leaving.. and maybe not even running the end of day script or closing out. or texting co-worker B, saying, dude i'm running late.. log me in to my pc. If this runs as a service as well, it can run in the background and the user cannot close it without access to services control panel.

you've created some great scripts here, but consider that people will always try to cheat the system..
Image
User avatar
tremor77
Contributor
Contributor
 
Posts: 860
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)


Re: Need Help With Batch Script

Post by WallShadow on Thu Aug 23, 2012 9:41 am
([msg=68951]see Re: Need Help With Batch Script[/msg])

@logical
Thanks for telling me after I'm 3/4 done with this thing :D. Unfortunately batch scripts aren't designed with a security log interface, although I've read that WMIC allows reading of security logs with the 'WMIC NTEVENTLOG GET * ' command, however I'm not nearly proficient enough with WMIC to be able to parse through it. If you do happen to know how to do this with WMIC, do tell.

@tremor
I'm making the script auto start when the user logs on and having it run as a background program, so none of the closing the window shenanigans are possible and unless they are explicitly told that they are being monitored, the only way they find out is if there is a tech genius snooping around who notices it running.

@jadecook
FWI, this is pushing the batch files way past the limits of what it was designed to do. I believe this would have been simpler in a language such as C.



In other news, anyone know how to start a batch file as a service? That would help me a tremendous lot.

-WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 594
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests