How to figure out how long a batch file is running?

February 12, 2012 at 12:54:12
Specs: Windows 7
Oops... I accidentally said "hoe" instead of "how"!

I have this line of code at the start:

set start_time=%TIME%
and i want a line of code that measures the time between %start_time% and the current time. I then want to translate it into seconds and have this code:
echo %tim%
//tim being the time between now and start.

Can you help? I was thinking of using the path variable alteration that is under "set /?" in cmd prompt but am not sure how this will work to my advantage....

Variants of PATH would be like(fyi type "set /?" in cmd and it will tell u all this[Very handy :) ]) :
%TIMEPATH:":"=% //this will delete ALL :

%TIMEPATH:~-2% // This will take the last 2 characters of the TIME var

%TIMEPATH:~7,2% // This will extract 2 characters starting from the 6th one. (TIME=HH:MM:SS.ML) (ML=milliseconds)

%TIMEPATH:~0,-5% // would extract all but the last 5 characters

Note: this works with any var (%!var!PATH% ect.)

Also I need a random number code! not:
set ran=%random%
cause that goes up to like 32,000ish. I need one that's 1-100 I tried:
set ran=%random%
if /i %ran% GTR 100 goto random
but that code takes between instant and like 1 minute. I want one that is REALLY fast!

See More: How to figure out how long a batch file is running?

February 12, 2012 at 16:58:21

What come to mind as a real down 'n dirty way to accomplish what you are trying to do, and this is to know how long a batch file ran for, is to create a blank dummy file at the begging of the batch file run and then at the end create another. You'll have a time stamp on the two files, the difference between the two times will give you the measurement you are looking for. Not a fancy way to get he job done, but it might save you a lot of time trying to find it.

Something like this can be done to accomplish :
at the beginning of the batch file - dir > start.txt
at the end of the batch - dir > stop.txt

The grass may not be greener on the other side but might have less weeds...
Web Based Desktop Support Software

Report •

February 12, 2012 at 17:05:03
Sorry but that won't work because the the FILE is supposed to tell you how long it runs. I can't go and check two blank files to see the amount of time! But thanks for trying to help...

Report •

February 13, 2012 at 06:08:26
Batch is not good for a lot of things, timedate math being one of them. The best bet would be to get a program that times how long a process runs, and use that. VBScript/JScript let you use WMI to get an approximate run time. With WIn7, you have access to PowerShell, so you should be able to get the exact start/stop time, but I'm unsure how difficult it would be.

How To Ask Questions The Smart Way

Report •

Related Solutions

February 13, 2012 at 16:16:54
sorry but the program has already been written up... I'm not gonna rewrite in in a language i don't know....

Report •

February 13, 2012 at 17:18:13
for random number, use this

set number=%random:~0,2%

it will give you numbers between 0-99.

For the time Comparision, As Razor said cmd is not a good way to go about.

Since you decided to go for it, here is a bad way.
step 1 : create a timer.bat in the same folder where you original batch is
@echo off
set _time=1
echo %_time% >timer.txt
ping -w 1 -n 1 >nul
set /a _time=_time+1
goto 1
step 2. at the first line of your original batch file, call this batch and let it run minimize, start /n i mean

step 3. this batch file will keep writing the current progress in seconds in the same folder as timer.txt (Can change to a variable instead if you want to)

step 4. at the last line of your batch, read timer.txt from the same foler, it will have the correct time in second till the time batch was running.
step5. At the end you might have to call a taskkill /im cmd.exe /f to kill all the running batch files, i dont see an option to kill this particular instance until you are gonna use Vbscript.

Shoot back your question if any.

- Subhash
 Monthly giveaway -  Must have Online safety Guide

Report •

February 14, 2012 at 04:17:14
In batch:
SET /A Random_Number_1_to_100=%RANDOM% %%100 + 1

Command line test:

SET /A %RANDOM% %100 + 1

When your only tool is a hammer, every problem looks like a nail.

Report •

February 18, 2012 at 22:33:08
@echo off

:: Store start time
FOR /f "tokens=1-4 delims=:.," %%T IN ("%TIME%") DO (
SET /a Start100S=%%T*360000+%%U*6000+%%V*100+%%W

:: Your stuff Goes below this

::Your stuff goes above this

:: This is the Timer ending output

FOR /f "tokens=1-4 delims=:.," %%T IN ("%TIME%") DO (
SET /a Stop100S=%%T*360000+%%U*6000+%%V*100+%%W
:: Test midnight rollover. If so, add 1 day=8640000 1/100ths secs
IF %Stop100S% LSS %Start100S% SET /a Stop100S+=8640000
SET /a TookTime=%Stop100S%-%Start100S%
::(%~nx0) this goes in between echo and started etc.
ECHO Started: %StartTime%
ECHO Stopped: %StopTime%
ECHO Elapsed: %TookTime:~0,-2%.%TookTime:~-2% seconds

Then for the random number that you wanted try this

set /a number=%random% %% 8

in this script it will find a random number from 0 to 8 "8" can be changed to what ever number u like up to about 32000

I think that is more what you were looing for right?

Report •

Ask Question