Get a random sentence from a selection each time in Batch - windows

Is there a way of making it so instead of saying the same echo that you set every time, you can give a list of echos and it chooses a random one to say each time it reaches that echo command?

Yep. Here's a proof of concept.
#echo off
setlocal enabledelayedexpansion
set string[0]=This is the first random line.
set string[1]=This is the second random line.
set string[2]=This is the third random line.
set /a idx=%random% * 3 / 32768
echo !string[%idx%]!
Here's more info on generating random numbers in Windows batch scripting.

#echo OFF
SETLOCAL
SET message0=message zero
SET message1=message one
SET message2=message two
SET message3=message three
SET message4=message four
:: running 10 times
FOR /l %%i IN (1,1,10) DO CALL :showme
GOTO :eof
:showme
SET /a select=%RANDOM% %% 5
CALL SET message=%%message%select%%%
ECHO %message%
GOTO :eof

Related

batch file extra iteration into only one label

Sorry for the length but the error I'm getting, possibly, encompasses all of my code.
I've made two files, one(rngInputTest) starts and checks for execution looping and passes four variables to the second(rngCompare) file via parameters in the call command.
The second file then takes the four parameters and saves them to four variables local to rngCompare, then uses %random% twice. After that it sets the two new variables(rng1 and rng2) into compare var by adding them together and passes compare back to rngInputTest has a parameter which is saved to the var result.
rngInputTest then does a final check and if the checkLoop var is 2 it goes to the final label/function(rngInputTestDisplay),which works, it echoes result fine.
The problem I'm having is that ,without my workaround, somehow execution goes to rngInputTestDisplay a second time after rngInputTestDisplay is ran once(no other label/function is called and when rngInputTestDisplay is ran the second time it empties the result var)
My current workaround is
IF %checkLoop%==%logicIII% goto :end
I placed this at the beginning of rngInputTestDisplay and it stops the extra iteration.
Without the workaround,even after putting:
echo in [label]
pause
in all of the labels/functions,in rngInputTest and rngCompare, I still wasn't able to figure out how the extra iteration is occurring though I did notice that execution loops only to rngInputTestDisplay none of the earlier labels were executed.
The end goal for these two files is to create a re-usable random num gen,so that I can call the first one multiple-times and pass different sets of data to it then return that data back to the 'main' file. I know I could use %random% once and save myself the headache but after using it a bit it doesn't really feel that random
I've included both files below
rngInputTest.bat
#ECHO off
::increment the loop-check var
set /a checkLoop+=1
goto :initLoopCheck
::used to check for looping
:initLoopCheck
::before execution loops to rngCompare
set logicI=1
::after execution loops back from rngCompare
set logicII=2
::used in rngInputTestDisplay,phantom loop
set logicIII=3
::starts the initial loop
IF %checkLoop%==%logicI% goto :rngInputTestInitVarSet
::checks if code has looped back from rngCompare.bat
IF %checkLoop%==%logicII% goto :logic
::sets all the non-increment variables
:rngInputTestInitVarSet
::min-max's for use in rngCompare.bat :randomGen
set minI=0
set maxI=50
set minII=0
set maxII=50
goto :logic
::checks to see how far checkLoop has incremented
:logic
IF %checkLoop%==%logicI% goto :rngInputTestPass
IF %checkLoop%==%logicII% goto :rngInputTestDisplay
::passes min-max's to rngCompare via parameters
:rngInputTestPass
set checkLoop+=1
call rngCompare.bat %minI% %minII% %maxI% %maxII%
goto :rngInputTestDisplay
:rngInputTestDisplay
::this IF is important
::without it the code would loop..only to
:: :rngInputTestDisplay
::which causes result to be empty
IF %checkLoop%==%logicIII% goto :end
set /a checkLoop+=1
set result=%~1
echo result:%result%
pause
:end
rngCompare.bat
#ECHO off
goto :rngCompareInitVarSet
::initial variable set
:rngCompareInitVarSet
set MaxI=0
set MaxII=0
set MinI=0
set MinII=0
set result=0
goto :afterPass
::after rngInputTestPass
:afterPass
set MinI=%~1
set MinII=%~2
set MaxI=%~3
set MaxII=%~4
goto :randomGen
::generate two random numbers
:randomGen
::gets two random numbers from the variables passed from rngInputTest.bat
set /a rngI=(%RANDOM%*%MaxI%/32768)+%MinI%
set /a rngII=(%RANDOM%*%MaxII%/32768)+%MinII%
goto :compareRNG
::add the two rng numbers
:compareRNG
::adds the two numbers together
set /a compare=%rngI%+%rngII%
goto :passToRNGInputTest
::pass the compare var back to rngInputTest
:passToRNGInputTest
call rngInputTest.bat %compare%
Way too complicated. You don't have to "pass" values back and forth. You already are setting them in Environment Variables and the second batch file runs from the same environment so anything updated in one will be available to the other.
For example these two simple batch files will show you the values are available.
myFirst.bat
#echo off
set /a myVal1=1
set /a myVal2=2
set /a myVal3=3
set /a myVal4=4
call mySecond.bat
echo my updated values from second batch
echo %myVal1%
echo %myVal2%
echo %myVal3%
echo %myVal4%
pause
mySecond.bat
#echo off
echo my values created in first batch
echo %myVal1%
echo %myVal2%
echo %myVal3%
echo %myVal4%
set /a myVal1=%myVal1%+%Random%
set /a myVal2=%myVal2%+%Random%
set /a myVal3=%myVal3%+%Random%
set /a myVal4=%myVal4%+%Random%
pause
If you call the second one more than once, just let it end, don't call the first file again. When the second bat finishes it will return from the Call to the first file.

How can I run a command with a variable? /// How can I produce a random number with with a variable?

I'm aware that set zeroThroughNine=%Random%*9/32768 followed by echo %zeroThroughNine% will produce a a random number between and including 0 and 9. But it seems the interpreter doesn't evaluate the contents of the variable every time it is called, and as such, echo %zeroThroughNine% produces, for example, 7 every time.
I looked up a method for running commands using variables so that I could try to force it to work. I liked the question because it was very basal in its approach; something along the lines of "How can I run commands using variables?", tagged appropriately. I didn't much care for the answer because it was very narrow. The highest voted and selected answer was:
Simple. Just run set commandVar=echo "Hello world.", followed by echo %commandVar%.
Of course the truth is that only works for the echo command. >: [
Anyway I'll stop complaining. This is what I've tried:
set zeroThroughNine=set /a number=%Random%*9/32768 & echo %number% followed by echo %zeroThroughNine%
Unfortunately the & echo %number% section of my SET command runs immediately, producing "%number%" as output --and using echo %zeroThroughNine% produces "set /a number=8436*9/32768", for example, as output.
So two questions: How can I universally achieve running commands with the use of variables (or some alternative method), and perhaps more pressing, how can I achieve producing a new random number at the command line with each new command calling?
You should set number before you set zeroThroughNine to the command, like so:
set /a number=%Random%*9/32768
set zeroThroughNine=echo %number%
%zeroThroughNine%
Also, since zeroThroughNine already is an echo command, you don't need to add the extra echo before it.
EDIT:
Taking into account your Random calculation is needlessly complicated, the final code should be something like this (1 - 10 exclusive):
set /a number=%Random% %% 10
set zeroThroughNine=echo %number%
%zeroThroughNine%
Important thing is, rather than trying to do it all on one line, it is much more readable by separating it into two.
The CALL SET syntax allows a variable substring to be evaluated, the CALL page has more detail on this technique, in most cases a better approach is to use Setlocal EnableDelayedExpansion.
Command line (note that all % percent signs are escaped as ^% and that > and & characters are escaped within a pair of " double quotes:
set "zeroThroughNine=call set /a number=^%Random^% ^% 10>nul & call echo number=^%number^%"
%zeroThroughNine%
for /L %G in (1, 1, 10) do #%zeroThroughNine%
Batch script, CALL method (note that all % percent signs are escaped as %%):
#echo OFF
SETLOCAL
set "_zeroThroughNine=call set /a _number=%%Random%% %%%% 10 & call echo number=%%_number%%"
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
Batch script, EnableDelayedExpansion only for output:
#echo OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_zeroThroughNine=set /a _number=!Random! %% 10 & echo Number=!_number!"
SETLOCAL EnableDelayedExpansion
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
ENDLOCAL
Batch script, EnableDelayedExpansion script wide (note that ! exclamation sign is escaped as ^!):
#echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_zeroThroughNine=set /a _number=^!Random^! %% 10 & echo NUMBER=^!_number^!"
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
Check out this question. Basically, it's an issue with how the %random% environment variable works...
EDIT:
To elaborate, the reason your random value was always 7 is because of how cmd's pseudo-random number generator works, not because of how the variables are interpreted. The matter is explained very well in this answer.
Essentially, in repeated runs of a batch file, %RANDOM% will produce a value very close to the previous run. Thus, the expression %RANDOM%*9/32768 produces the same result in each separate run because of the random value.
If I understand correctly, the question you're asking is how to better generate a random value 0 - 9 inclusive, which would be by using the following expression:
set /a zeroThroughNine=%RANDOM% %% 10

Command Prompt Search for files in a directory take the name of one random

Inside a directory c:\configs I have files with various extensions including the extension .rac. I need a script for the Windows command prompt that looks inside c:\configs for the names of the files that end with the extension .rac, ignoring other extensions. Then of all the names that end with .rac extension the script must choose a random one and process it with the command c:\programs\submit.exe namerandom.rac.
For example, suppose that random .rac file is called mosaic.rac, then the script executes the command c:\programs\submit.exe mosaic.rac. Of course the mosaic.rac name changes each time the script is runs because it is a random selected from the all the .rac files found.
Anyone have an idea in how to do and that can put example code?
#echo off
setlocal EnableDelayedExpansion & set n=0
for /f "delims=" %%a in ('dir /b /A-D "*.rac"') do (
set "f=%%a" & set "f[!n!]=!f!" & set /a "n+=1")
set /a c=%random% %% n
echo !f[%c%]!
Explanation:
Line #4: it make a pseudo array in f with n incremented by 1
Line #5: it take a random number between 0 and the total count of files called n with the help of: %random% modulo n
In this way, this creates a number of variables automatically according to their position then %random% %% n picks one.
You might as well picks some manually like this:
echo !f[0]! !f[1]! !f[2]! !f[3]! !f[4]! !f[5]! ...
To accomplish that, you may use the following...
Firstly, to get all .rac files, use the dir command. The /B switch specifies to output only a bare file list without any headers nor footers. If you want to search the given directory recursively, add the /S switch:
dir /B "C:\configs\*.rac"
Secondly, you need to count the number of returned .rac files. You can use a for /F loop (parsing the output of dir /B) together with set /A for that:
set /A COUNT=0
for /F "delims=| eol=|" %%L in (
'dir /B "C:\configs\*.rac"'
) do (
set /A COUNT+=1
)
Thirdly, you need to compute a random number in the applicable range. The built-in variable RANDOM retrieves a random number from 0 to 32767, so we need a little maths. The result will be a number from 0 to %COUNT% - 1:
set /A RNDNUM=%RANDOM%%%COUNT
Fourthly, you can use another for /F loop with the skip option to select a random file (we skip the previously calculated number RNDNUM of lines).
The if statement ensures that no skip option is provided in case the random number is 0.
The goto command ensures that only the selected file is passed to submit.exe. If you omitted it, every file after the selection would be passed over to submit.exe too, one after another:
if %RNDNUM% gtr 0 (
set SKIP=skip=%RNDNUM%
) else (
set SKIP=
)
for /F "%SKIP% delims=| eol=|" %%L in (
'dir /B "C:\configs\*.rac"'
) do (
start "" /WAIT "submit.exe" "%%~L"
goto :CONTINUE
)
:CONTINUE
Put together those parts to get the final script.
Type each command and append /? in command prompt to get the respective help text displayed.

How do I get truly random numbers in batch?

Everyone uses random numbers at one point or another. So, I need some truly random numbers (not pseudo-random*) generated from the command prompt, I also want a system that generates letters in a code line the size of: set RANDOM=%random% %%2 +1. And am I just trying to catch a non-existent butterfly? *pseudo-random is random that relies on outside info like time, batch's random generator is pseudo-random based on time, to test this open 2 new notepad .bat and name this file 1.bat
1.bat
start 2.bat
#echo off
cls
echo %random%
pause
2.bat
#echo off
cls
echo %random%
pause
What's happening?!? Well this is pseudo-random, as long as there is NO delay between the opening of the batch files, the batch file's numbers will be the same.
You're basically asking for entropy pool, and your platform is Windows, right?
Best bet is to use CryptGenRandom() function, see https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx. You could probably call it from Powershell
UPDATE
Apparently, there is a .NET wrapper for crypto, so you could use it from Powershell
[System.Security.Cryptography.RNGCryptoServiceProvider] $rng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$rndnum = New-Object byte[] 4
# Generate random sequence of bytes
$rng.GetBytes($rndnum)
# rndnum is filled with random bits
....
On powershell it's pretty easy,
You can use the cmdlet Get-Random by itself or specify range like 1..100 | Get-Random
Another option is to call the System.Random object directly:
$Random = New-Object System.Random
$Random.Next()
Well, you may program a Batch file version of one of the tons of existent methods that generate pseudo-random numbers, or look for one that may be programmed in a Batch file with no further complications like The Minimal Standard Random Number Generator, or a simpler version of it like these Two Fast Implementations, and even modify one of they to made it simpler!
#echo off
setlocal EnableDelayedExpansion
rem Pseudo random number generator based on "The Minimal Standard"
rem http://www.firstpr.com.au/dsp/rand31/p87-carta.pdf
rem Antonio Perez Ayala aka Aacini
rem Initialize values
set /A a=16807, s=40
rem APA Modification: use current centiseconds for initial value
for /F "tokens=2 delims=." %%a in ("%time%") do if "%%a" neq "00" set /A s=1%%a %% 100
rem Example of use: generate and show 20 random numbers
for /L %%i in (1,1,20) do (
call :RandomGen
echo !rand!
)
goto :EOF
:RandomGen
rem Multiply the two factors
set /A s*=a
rem If the result overflow 31 bits
if %s% lss 0 (
rem Get result MOD (2^31-1)
set /A s+=0x80000000
)
rem APA modification: return just the low 15 bits of result (number between 0..32767)
set /A "rand=s & 0x7FFF"
exit /B
Of course, I know this method is not comparable with most of the "standard" methods, but I think it is enough for simple Batch file applications, like games...
The problem indeed is that %RANDOM% is only pseudo-random and depends when it's called; if two scripts use it (more-less) at the same time, it will have the same value.
Instead, call PowerShell from your batch script:
#for /f "tokens=*" %%i in ('powershell -Command "(1..100 | Get-Random)"') do #(
set "trueRandomInteger_between_1_and_100=%%i"
)

How long does a batch file take to execute?

How can I write a script to calculate the time the script took to complete?
I thought this would be the case, but obviously not..
#echo off
set starttime=%time%
set endtime=%time%
REM do stuff here
set /a runtime=%endtime%-%starttime%
echo Script took %runtime% to complete
Two things leap out about the original batch file. but neither is going to help in the long run.
Whatever your benchmark method, be sure to capture the start time before the operation, and the end time after the operation. Your sample doesn't do that.
%time% evaluates to something like "12:34:56.78", and the SET /A command can't subtract those. You need a command that produces a simple scalar time stamp.
I was going to say it can't be done, but the batch language is a lot more powerful than it is given credit for, so here is a simple implementation of TIMER.BAT. For the record, Pax beat me to an answer showing the string splitting while I was fiddling around, and Johannes Rössel suggested moving the arithmetic outside of the measured region:
#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
You could simplify the arithmetic and be a little more honest about the overall accuracy of this by not bothering with the 100ths of a second part. Here it is in action, assuming you have a sleep command or some other time waster:
C:> TIMER SLEEP 3
Script took 3000 ms to complete
C:>
Edit: I revised the code and its description as suggested in a comment.
I think that when the NT team replaced COMMAND.COM with CMD.EXE, they thought they wouldn't get away with making it very different. But in effect, it is almost an entirely new language. Many of the old favorite commands have new features if the extensions are enabled.
One of those is SETLOCAL which prevents variables from modifying the caller's environment. Another is SET /A which gives you a remarkable amount of arithmetic. The principle trick I've used here is the new substring extraction syntax where %t:~3,2% means the two characters starting at offset 3 in the value of the variable named t.
For a real shocker, take a look at the full description of set (try SET /? at a prompt) and if that doesn't scare you, look at FOR /? and notice that it can parse text out of files...
Edit 2: Fixed mis-handling of time fields containing 08 or 09 reported by Frankie in comments. Tweaked a couple of things, and added some comments.
Note that there is a glaring oversight here that I'm probably not going to fix. It won't work if the command starts on a different day than it ends. That is, it will do some math related to the time of day and report a difference, but the difference won't mean much.
Fixing it to at least warn about this case is easy. Fixing it to do the right thing is harder.
Edit 3: Fixed error where the %h% is not set properly for single digit hours. This is due to %time% returning " 9:01:23.45". Notice the space. Using %time: =0% replaces the space with a leading zero and %h% will be set correctly. This error only occurred when a script ran from one single digit hour to the next.
This is kind of tangential, but it may help you out.
Microsoft has a timeit.exe program that works more or less like an enhanced version of the unix 'time' command. It comes in the Windows Server 2003 Resource Kit, and it has pretty much replaced all the times I've wanted to do something like what you're suggesting.
It may be worth a look.
Excellent little routine. However, if the calculation spans across two days, the calculation is incorrect.
The result needs subtracting from 24 hours (8640000 centiseconds).
Also remove one of the duplicate 'set' commands in the relevant line.
Note that regional settings affect the format of the TIME function, the decimal being a fullstop in UK rather than a comma.
The lines
rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%
need changing to
rem we might have measured the time across days
if %ENDTIME% LSS %STARTTIME% set /A DURATION=8640000 - (%STARTTIME% - %ENDTIME%)
My own personal preference is to install Cygwin and use the time command but, if you actually have to do it as a batch file, you can't just subtract the strings, you have to treat them as parts.
The following script will time a 10-second ping command with the ability to cross a single day boundary without getting tied up in negative numbers. A slight enhancement would allow it to cross many day boundaries.
#echo off
setlocal enableextensions enabledelayedexpansion
set starttime=%time%
ping -n 11 127.0.0.1 >nul: 2>nul:
set endtime=%time%
set /a hrs=%endtime:~0,2%
set /a hrs=%hrs%-%starttime:~0,2%
set /a mins=%endtime:~3,2%
set /a mins=%mins%-%starttime:~3,2%
set /a secs=%endtime:~6,2%
set /a secs=%secs%-%starttime:~6,2%
if %secs% lss 0 (
set /a secs=!secs!+60
set /a mins=!mins!-1
)
if %mins% lss 0 (
set /a mins=!mins!+60
set /a hrs=!hrs!-1
)
if %hrs% lss 0 (
set /a hrs=!hrs!+24
)
set /a tot=%secs%+%mins%*60+%hrs%*3600
echo End = %endtime%
echo Start = %starttime%
echo Hours = %hrs%
echo Minutes = %mins%
echo Seconds = %secs%
echo Total = %tot%
endlocal
Check out this script that will retrieve time through WMI so it's Regional Setting independent.
#echo off
::::::::::::::::::::::::::::::::::::::::::
::  TimeDiff v1.00 by LEVENTE ROG       ::
::       www.thesysadminhimself.com     ::
::::::::::::::::::::::::::::::::::::::::::
 
::[ EULA ]:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::  Feel free to use this script. The code can be redistributed  ::
::  and edited, but please keep the credits.                     ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
::[ CHANGELOG ]::::::::::::::
::  v1.00 - First Version  ::
:::::::::::::::::::::::::::::
 
 
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
 set Milisecond=%time:~9,2%
 set Day=%%A
 set Hour=%%B
 set Minute=%%C
 set Second=%%D
)
set /a Start=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
 
::
::
:: PUT COMMANDS HERE
ping www.thesysadminhimself.com
::
::
 
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
 set Day=%%A
 set Hour=%%B
 set Minute=%%C
 set Second=%%D
)
set Milisecond=%time:~9,2%
set /a End=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
set /a Diff=%End%-%Start%
set /a DiffMS=%Diff%%%100
set /a Diff=(%Diff%-%DiffMS%)/100
set /a DiffSec=%Diff%%%60
set /a Diff=(%Diff%-%Diff%%%60)/60
set /a DiffMin=%Diff%%%60
set /a Diff=(%Diff%-%Diff%%%60)/60
set /a DiffHrs=%Diff%
 
:: format with leading zeroes
if %DiffMS% LSS 10 set DiffMS=0%DiffMS!%
if %DiffSec% LSS 10 set DiffMS=0%DiffSec%
if %DiffMin% LSS 10 set DiffMS=0%DiffMin%
if %DiffHrs% LSS 10 set DiffMS=0%DiffHrs%
 
echo %DiffHrs%:%DiffMin%:%DiffSec%.%DiffMS%
You can't do time arithmetic directly in batch scripting, so you'll either need an external program to calculate the time difference, or extract each part of the time and do arithmetic that way.
Take a look at the bottom of this forum thread for an example of how to do the latter. Excerpted here:
#echo off
cls
REM ================================================== ======
REM = Setting Date Time Format =
REM ================================================== ======
set DT=%DATE% %TIME%
set year=%DT:~10,4%
set mth=%DT:~4,2%
set date=%DT:~7,2%
set hour=%DT:~15,2%
set min=%DT:~18,2%
set sec=%DT:~21,2%
set newDT=%year%_%mth%_%date% %hour%%min%%sec%
set hour=14
set min=10
REM ===============================
REM = Getting End Time =
REM ===============================
set EndTime=%TIME%
set EndHour=%EndTime:~0,2%
set EndMin=%EndTime:~3,2%
REM ===============================
REM = Finding Difference =
REM ===============================
set /a Hour_Diff=EndHour - hour >nul
set /a Min_Diff=EndMin - min >nul
REM ===============================
REM = Hour Hand Change? =
REM ===============================
IF [%Hour_Diff]==[0] (
Set Duration=%Min_Diff%
) ELSE (
Set /a Duration=60-%Min_Diff% >nul
)
echo Start Time = %hour% : %min%
echo End Time = %EndHour% : %EndMin%
echo.
echo Min Diff = %Min_Diff%
echo.
echo time difference (mins) = %Duration%
I also assume it's a typo, but you do want to be sure to set endtime after processing.

Resources