Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am wanting a simple bat program when you type a name (Mike) into it is will export the current time into a txt document (Mike.txt) and then when he types Mike again it puts the time again in the text document. After that i want it to be able to show how many hours he worked. IF this is possible.
EX: in the text document (Mike.txt)
1:15 - 6:15 (5:00)Thanks!
p1ng

Here is what i have so far.
@ECHO OFF
REM _START
:_start
cls
echo To sign in enter 1
echo To sign out enter 2#####################
I am having a brain fart
and all i need is if they
press 1 it goes to next
and if they press 2
it goes to false1
I usually dont have problems like this
I have tried the way i thought worked
but it only went to next not false1
#######################################REM NEXT
:next
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo Sign in: %date% %time% >> MikeTime.txt
echo You are signed in.
pause
GOTO _start
REM FALSE1
:false1
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo Sign Out: %date% %time% >> MikeTime.txt
echo. >> MikeTime.txt
echo You are signed out.
pause
GOTO _startpause
EXIT

#####################
I am having a brain fart
and all i need is if they
press 1 it goes to next
and if they press 2
it goes to false1
I usually dont have problems like this
I have tried the way i thought worked
but it only went to next not false1
#######################################
:usrinp
set /p inp=
if %inp%==1 goto next
if %inp%==2 goto false1
goto usrinp

Thanks Judago
I thought i was doing that correctly but its been awhile since i made a bat program. It works good.

Is there anyway to have it display the number of hours from signing in to signing out?
This is what the text document currently displays:Sign in: Wed 10/01/2008 14:02:06.46
Sign Out: Wed 10/01/2008 14:03:04.12
2:01:02.34
Sign in: Wed 10/01/2008 14:02:26.45
Sign Out: Wed 10/01/2008 16:02:28.64
4:00:02.19I would like it to display
Sign in: Wed 10/01/2008 14:02:06.46♦
Sign Out: Wed 10/01/2008 16:02:06.46
2:00:00.00
Sign in: Wed 10/01/2008 14:02:26.45
Sign Out: Wed 10/01/2008 18:02:28.64
2:00:02.19

I don't think you'l have much luck doing math on the time.
Nobody here has.
=====================================
If at first you don't succeed, you're about average.M2

Time/date math in batch is a tedious and annoying task. It's by no means impossible, it just depends how much you want it.
Most people forget that to do seemingly simple math on time/dates in batch properly you must take into account several factors.
1. Date and time functions and variables can be affected by user setting from the windows enviroment. This can mean your script may work on your computer, but may not work on <preson>'s computer.
2. You must consider that days do start and end; a comprehensive solution must account for leap years and the length of each month.
3. Because batch, unlike most programming languages, doesn't have built in date functions you a relegated to using environment variable substitution to get the hour, minutes and seconds into a form you can perform arithmetic on, bearing in mind the set /a command does not tolerate leading zero's on decimal numbers, instead it considers any numbers with leading zero's that doesn't include and x as octal numbers making 08 and 09 invalid numbers.
If you still want to do date math, by all means do it, if you make something that works it will, hopefully, keep working with little maintenance.
Here's a few things I think will help you if you choose to go on:
Using prompt isn't your only option for time and date output. It is possible to use "built in" %time% and %date% variables, as mentioned above their format can vary depending on your settings. Another option for time and date output is to use either the time /t or date /t commands within a for /f loop.
I'd say your best bet to get a consistent output is to set variable=%time% so you can process a static time stamp rather than work on something that is still ticking.
Environment variable substitution is a very useful tool in these sorts of situations, basically you can use it to output parts of a variable. It's format is alone the lines of %variable:~0,1% this gives the first character of the variable. The first number is the offset, the second is how many characters it will continue after the offset. If only 1 number is specified then the rest of the variable is assumed after it's off set. It is also possible to use negitive numbers, the help screen for set gives more information on this tool.
As mentioned above set /a won't tolerate leading zero's, but if statements do so if var=09 the command: if %var% lss 10 set var=%var:~-1% will give you something you can add to, just remember you may need to add the leading zero after the math eg if %var% lss 10 set var=0%var%.
Recomended reading:
set /?
for /?

I am little late, but I had a timer batch file written to test how long some tasks take..
Hope the time math functions there are helpful for others.
Here is my timer.bat:
@echo off
setlocal EnableDelayedExpansion
If %1a==a goto Usage
set starttime=%time%
call :ExtractHMSHn %starttime% StartTimeInNum
call %*
set endtime=%time%
call :ExtractHMSHn %endtime% EndTimeInNum
:find the Difference in time
set /a diff = %EndTimeInNum% - %StartTimeInNum%
Set /a Elapsedhrs= %diff% / (60 * 60 * 100)
Set /a Elapsedmin= (%diff% - (%Elapsedhrs% * 60*60*100)) / 6000
set /a elapsedsectmp= %diff% - (%Elapsedhrs% * 60*60*100 ) %% 6000
set /a elapsedsec= %elapsedsectmp% / 100
set /a elapsedhun= %elapsedsectmp% %% 100
Echo Elapsed time: %Elapsedhrs%:%Elapsedmin%:%elapsedsec%.%elapsedhun%
goto :EOF
:Usage
Echo Usage: %0 YourCommand [Your command's parameters]
Echo Example: %0 ping -n 2 127.0.0.1
echo - will execute "ping -n 2 127.0.0.1" and tells how long it for execution.
goto :EOF:ExtractHMSHn
set t1=%1
set hun=%t1:~9,2%
set sec=%t1:~6,2%
set min=%t1:~3,2%
set hr=%t1:~0,2%
:Remove zero from the begining so that it does not crib about invalid octal
if %hun:~0,1%==0 set hun=%hun:~1,1%
if %sec:~0,1%==0 set sec=%sec:~1,1%
if %min:~0,1%==0 set min=%min:~1,1%
if %hr:~0,1%==0 set hr=%hr:~1,1%
set /a %2 = %hun% %% 100 + %sec% * 100 + %min% * 60*100 + %hr% * 60*60*100
exit /b
endlocal--
Holla.

I saved this exactly as a bat file.
On my computer it opens then closes instantly and nothing else happens... no file created.
any ideas?

p1ng,
If you saved it as a batch file and try to launch it by double clicking on it.
It will flash the usage screen and exit.
It happens so quick that you may not notice.
Ideally it should be executed from the command prompt, like
timer.bat ping -n 3 127.0.0.1
here timer.bat is the batch file name
and "ping -n 3 127.0.0.1" is the command you are trying to figure out how long it takes.You may also add a "pause" command just prior to the statement "goto :EOF"
(both places). That will make the program to pause for user input before exiting.--
Holla.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |