Solved

Need a batch script with Timer

Posted on 2014-02-28
5
561 Views
Last Modified: 2014-05-19
I have a XCOPY script in which I would like to know how long it took to execute. This timer information can just be sent to a LOG file, for ease and historical information.

Can you build a timer around my script?

xcopy \\CGIGEHRSQLCLU02\h$\DBBackup C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\ /S /C /Q /Y /Z /J
xcopy \\Cgigehrsqlclu02\h$\DBBackup\IGEHR_PROD\*.BAK C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\EIGRP_PROD /S /C /Q /Y /Z /J
xcopy \\Cgigehrsqlclu02\h$\DBBackup\IGEHR_PROD_Imaging\*.bak C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\IGEHR_PROD_Imaging  /S /C /Q /Y /Z /J

Open in new window

0
Comment
Question by:Glenn M
5 Comments
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
Comment Utility
It could be something as simple as this?

@echo off
echo Start Step 1 %time% >> <drive>:\<folder>\<filename>
xcopy \\CGIGEHRSQLCLU02\h$\DBBackup C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\ /S /C /Q /Y /Z /J
echo Start Step 2 %time% >> <drive>:\<folder>\<filename>
xcopy \\Cgigehrsqlclu02\h$\DBBackup\IGEHR_PROD\*.BAK C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\EIGRP_PROD /S /C /Q /Y /Z /J
echo Start Step 3 %time% >> <drive>:\<folder>\<filename>
xcopy \\Cgigehrsqlclu02\h$\DBBackup\IGEHR_PROD_Imaging\*.bak C:\BE.UNC.BACKUPS\SQL.BAK.TEMP\IGEHR_PROD_Imaging  /S /C /Q /Y /Z /J
echo Job complete %time% >> <drive>:\<folder>\<filename>

Open in new window

0
 
LVL 16

Expert Comment

by:choward16980
Comment Utility
I use this a lot:

http://stackoverflow.com/questions/739606/how-long-a-batch-file-takes-to-execute


@echo off
setlocal

rem Remeber start time. Note that we don't look at the date, so this
rem calculation won't work right if the program run spans local midnight.
set t0=%time: =0%

rem do something here.... but probably with more care about quoting.
rem specifically, odd things will happen if any arguments contain 
rem precent signs or carets and there may be no way to prevent it.
%*

rem Capture the end time before doing anything else
set t=%time: =0%

rem make t0 into a scaler in 100ths of a second, being careful not 
rem to let SET/A misinterpret 08 and 09 as octal
set /a h=1%t0:~0,2%-100
set /a m=1%t0:~3,2%-100
set /a s=1%t0:~6,2%-100
set /a c=1%t0:~9,2%-100
set /a starttime = %h% * 360000 + %m% * 6000 + 100 * %s% + %c%

rem make t into a scaler in 100ths of a second
set /a h=1%t:~0,2%-100
set /a m=1%t:~3,2%-100
set /a s=1%t:~6,2%-100
set /a c=1%t:~9,2%-100
set /a endtime = %h% * 360000 + %m% * 6000 + 100 * %s% + %c%

rem runtime in 100ths is now just end - start
set /a runtime = %endtime% - %starttime%
set runtime = %s%.%c%

echo Started at %t0%
echo Ran for %runtime%0 ms

Open in new window



It's pretty extensive and friendly with the notation
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
I've got a bit of a shorter one I have written here if you want too:

http://scripts.dragon-it.co.uk/links/batch-find-time-taken

Steve

REM =======================================
REM Set of subroutines to make time differences easier in batch files using VBScript to do the maths.
REM Steve Knight, Aug 2012
REM http://scripts.dragon-it.co.uk/
REM =======================================
REM Call :START to "Start the clock"
REM Call :END to "Stop the clock"
REM Call :ShowDiff x to show the difference where x is s, n, h for secs, mins, hours
REM Once STARTed you can do multiple ENDs or do another START to reset
REM =======================================

REM =======================================
REM Examples
REM =======================================
call :START
echo Waiting
ping 127.0.0.1 -n 4 > NUL
call :END
call :ShowDiff s

call :START
echo Waiting
ping 127.0.0.1 -n 45 > NUL
CALL :END
call :ShowDiff s

echo Continuing
ping 127.0.0.1 -n 90 > NUL
CALL :END
call :ShowDiff n

EXIT/B
REM =======================================
REM Subroutines below here
REM =======================================
:START
set start=%date% %time:~0,8%
echo START at %start%
exit /b

:END
set end=%date% %time:~0,8%
echo END at %end%
exit /b

:ShowDiff
REM Call with s,n,h for seconds, mins, hours. defaults to secs
(set type=%~1)& if "%~1"=="" set type=s

echo Wscript.Echo DateDiff("%type%", #%start%#,#%end%# ) > "%temp%\timediff.vbs"
for /f %%s in ('cscript //nologo "%temp%\timediff.vbs"') do set TimeDiff=%%s
del "%temp%\timediff.vbs"
if %Type%==n set type=m
echo TIME TAKEN: %TimeDiff% %Type%
exit /b

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
I actually prefer to use a small EXE utility for this job, take a look at this freebie that does a nice job:

http://www.gammadyne.com/cmdline.htm#timer

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Sometimes wonder why you bother!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now