Advertisement
IcarusLives

Time Difference from Functions and Macros

Jun 10th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.49 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2. REM Records time before and after, displays how long it took
  3. call :init
  4. call :macros
  5.  
  6. :start
  7. set /a nP+=1
  8. call :text "(Running payload)" 0b 0
  9. echo.
  10. set tB=%time%
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.     REM ######  <Payload section>  ######
  18.        
  19.     if !np! equ 1 (
  20.         for /l %%a in (1,1,1000) do call :calculateAverage 32 73 62 25 19
  21.     ) else (
  22.         for /l %%a in (1,1,1000) do %calculateAverage% 32 73 62 25 19
  23.     )
  24.    
  25.     REM ###### </Payload Section>  ######
  26.  
  27.    
  28.    
  29.    
  30.    
  31.    
  32.    
  33. set tA=%time%
  34.  
  35. REM Trap potential leading spaces
  36. for %%a in (%tA%) do (set tA=%%a)
  37. for %%a in (%tB%) do (set tB=%%a)
  38.  
  39. REM Split to components
  40. for /f "tokens=1-4 delims=:." %%a in ("%tB%") do (
  41.     set bH=%%a
  42.     set bM=%%b
  43.     set bS=%%c
  44.     set bC=%%d
  45.     )
  46.  
  47. for /f "tokens=1-4 delims=:." %%a in ("%tA%") do (
  48.     set aH=%%a
  49.     set aM=%%b
  50.     set aS=%%c
  51.     set aC=%%d
  52.     )
  53.  
  54. REM Trap potential leading zeros
  55. if "%bH:~0,1%" EQU "0" set /a bH=%bH:~1,1% 2>nul
  56. if "%bM:~0,1%" EQU "0" set /a bM=%bM:~1,1%
  57. if "%bS:~0,1%" EQU "0" set /a bS=%bS:~1,1%
  58. if "%bC:~0,1%" EQU "0" set /a bC=%bC:~1,1%
  59.  
  60. if "%aH:~0,1%" EQU "0" set /a aH=%aH:~1,1% 2>nul
  61. if "%aM:~0,1%" EQU "0" set /a aM=%aM:~1,1%
  62. if "%aS:~0,1%" EQU "0" set /a aS=%aS:~1,1%
  63. if "%aC:~0,1%" EQU "0" set /a aC=%aC:~1,1%
  64.  
  65. REM Convert the hours, minutes, seconds and cents into just cents
  66. set /a eCT=(^
  67. %aC% + (%aS% * 100) + (%aM% * 6000) + (%aH% * 360000)) - (^
  68. %bC% + (%bS% * 100) + (%bM% * 6000) + (%bH% * 360000))
  69.  
  70. REM Trap for midnight
  71. if %eCT% LSS 0 (
  72.     set /a eCT+=8640000
  73.     )
  74.  
  75. REM Convert to hours, Minutes, Seconds and Cents
  76. call :toComponents %eCT%
  77.  
  78. REM Report the result
  79. cls
  80. call :text "Payload execution complete" 0b 0
  81. echo .
  82. for /l %%a in (1,1,6) do (echo.)
  83.  
  84. call :text "    Elapsed" 0d 0
  85. echo : %eH%:%eM%:%eS%.%t%
  86.  
  87. REM Calculate the average scan time
  88. set /a tT+=%eCT%
  89. set /a avg=%tT% / %nP%
  90. call :toComponents %avg%
  91.  
  92. REM Report the average scan time, except if this is first run
  93. REM call :text "    Average" 0e 0
  94. REM echo : %eH%:%eM%:%eS%.%t%
  95.  
  96. REM Show the number of executions
  97. if %nP% GTR 1 (
  98.     call :text "    Passes" 02 0
  99.     echo  : %nP%
  100.     )
  101. if %nP% EQU 1 echo.
  102. for /l %%a in (1,1,6) do (echo.)
  103.  
  104. call :text "Press any key to repeat" 08 0
  105. echo ...& echo.
  106. pause >nul
  107. goto :start
  108.  
  109. :init
  110.     set /a nP=0
  111.     set /a tT=0
  112.  
  113.     REM Initialise backspace variable, required for Text function
  114.     for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (set "z=%%a")
  115.  
  116.     call :text "Press" 03 0
  117.     call :text " any" 08 0
  118.     call :text " key to" 07 0
  119.     call :text " execute the" 0b 0
  120.     call :text " payload" 0f 0
  121.     echo ...
  122.  
  123.     pause >nul
  124.     cls
  125. goto :eof
  126.  
  127. :toComponents
  128.     set /a eH=0
  129.     set /a eM=0
  130.     set /a eS=0
  131.     set /a t=%1
  132.  
  133.     :hours
  134.     if %t% GTR 360000 (
  135.         set /a eH+=1
  136.         set /a t-=360000
  137.         goto :hours
  138.         )
  139.  
  140.     :minutes
  141.     if %t% GTR 6000 (
  142.         set /a eM+=1
  143.         set /a t-=6000
  144.         goto :minutes
  145.         )
  146.  
  147.     :seconds
  148.     if %t% GTR 100 (
  149.         set /a eS+=1
  150.         set /a t-=100
  151.         goto :seconds
  152.         )
  153.  
  154.     REM Add leading zeros as required
  155.     if "%eH:~1,1%" EQU "" set eH=0%eH%
  156.     if "%eM:~1,1%" EQU "" set eM=0%eM%
  157.     if "%eS:~1,1%" EQU "" set eS=0%eS%
  158.     if "%t:~1,1%" EQU "" set t=0%t%
  159. goto :eof
  160.  
  161. :text
  162. <nul set /p .=. > "%~1"
  163. findstr /v /a:%2 /R "^$" "%~1" nul
  164. if "%3" EQU "0" <nul set /p z=%z%%z%%z%
  165. if "%3" NEQ "0" echo %z%%z%%z%
  166. del "%~1" > nul 2>&1
  167. goto :eof
  168.  
  169. REM :calculateAverage FUNCTION
  170. :calculateAverage
  171.            if %5 gtr 0 ( set "div=5"
  172.     ) else if %4 gtr 0 ( set "div=4"
  173.     ) else if %3 gtr 0 ( set "div=3"
  174.     ) else if %2 gtr 0 ( set "div=2"
  175.     ) else if %1 gtr 0  set "div=1"
  176.     set /a "sum=%1 + %2 + %3 + %4 + %5", "avg=sum / div"
  177.     echo The average is !avg!
  178. goto :eof
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. :macros
  186. set ^"LF=^
  187.  
  188. ^" Above empty line is required - do not remove
  189. set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
  190.  
  191. REM %calculateAverage% function MACRO
  192. set calculateAverage=for %%# in (1 2) do if %%#==2 ( for /f "tokens=1-5" %%1 in ("^!args^!") do (%\n%
  193.            if %%5 gtr 0 ( set "div=5"%\n%
  194.     ) else if %%4 gtr 0 ( set "div=4"%\n%
  195.     ) else if %%3 gtr 0 ( set "div=3"%\n%
  196.     ) else if %%2 gtr 0 ( set "div=2"%\n%
  197.     ) else if %%1 gtr 0  set "div=1"%\n%
  198.     set /a "sum=%%1 + %%2 + %%3 + %%4 + %%5"%\n%
  199.     for /f "tokens=1,2" %%a in ("^!sum^! ^!div^!") do (%\n%
  200.         set /a "avg=%%a / %%b"%\n%
  201.         for /f %%0 in ("^!avg^!") do echo The average is %%0%\n%
  202.     )%\n%
  203. )) else set args=
  204. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement