Radom Time?

  • Thread starter Thread starter Jon Smith
  • Start date Start date
J

Jon Smith

I would like to create a variable in a batch file with random time between 2
values. For example I need a random time between 13:00:00 and 20:00:00, does
anyone know how to do that? I've been searching for a long time but haven't
seen anything yet... I guess a VBS would work as well...



thanks
 
Jon said:
I would like to create a variable in a batch file with random time between 2
values. For example I need a random time between 13:00:00 and 20:00:00, does
anyone know how to do that? I've been searching for a long time but haven't
seen anything yet... I guess a VBS would work as well...

Hi

Random time between 13:00:00 and 19:59:59 with a VBScript:

sRndTime = Random2DigitNumber(13, 19) & ":" _
& Random2DigitNumber(0, 59) & ":" & Random2DigitNumber(0, 59)

WScript.Echo sRndTime

Function Random2DigitNumber(iLower, iUpper)
Randomize
Random2DigitNumber = _
Right("00" & Int((iUpper - iLower + 1) * Rnd + iLower), 2)
End Function
 
In said:
I would like to create a variable in a batch file with random time
between 2 values. For example I need a random time between
13:00:00 and 20:00:00, does anyone know how to do that? I've been
searching for a long time but haven't seen anything yet... I guess
a VBS would work as well...

You might start with the value of %random% in NT5x

3rd-party: random.exe
http://www.optimumx.com/
 
I'm sorry to bug you again on this... but I'm not sure on how to pass things
between VB and batch files... maybe I don't' need a batch script at all...
this is what I'm trying to do.. I have a batch script that will make a
scheduled task that I need a random time on ... so this is what I have so
far, the (sRndTime) is where I need the random time.



"schtasks /create /tn Weekly /sc WEEKLY /d SUN /st (sRndTime) /tr
C:\weekly.bat /s %COMPUTERNAME%"



I have this in a VBS file right now...



========================
sRndTime = Random2DigitNumber(13, 19) & ":" _
& Random2DigitNumber(0, 59) & ":" & Random2DigitNumber(0, 59)

Function Random2DigitNumber(iLower, iUpper)
Randomize
Random2DigitNumber = _
Right("00" & Int((iUpper - iLower + 1) * Rnd + iLower), 2)
End Function

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "schtasks /create /tn Weekly /sc WEEKLY /d SUN /st %sRndTime%
/tr C:\backup\weekly.bat /s %COMPUTERNAME%", 0
==========================

now %sRndTime% is not working as a variable how would I pass the time into
the script?



thanks
 
Jon said:
(snip)
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "schtasks /create /tn Weekly /sc WEEKLY /d SUN /st %sRndTime%
/tr C:\backup\weekly.bat /s %COMPUTERNAME%", 0
==========================

now %sRndTime% is not working as a variable how would I pass the time into
the script?

Hi

instead of
.../d SUN /st %sRndTime% /tr C:\backup\weekly.bat...

you need to do like this
...d SUN /st " & sRndTime & " /tr C:\backup\weekly.bat...

Here is an updated version of the script ( _ tells the vbscript interpreter that
the script continues on the next line, this to avoid line break issues when
posting through a newsreader):


sRndTime = Random2DigitNumber(13, 19) & ":" _
& Random2DigitNumber(0, 59) & ":" & Random2DigitNumber(0, 59)

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "schtasks.exe /create /tn Weekly /sc WEEKLY /d SUN /st " _
& sRndTime & " /tr C:\backup\weekly.bat /s %COMPUTERNAME%", 0


Function Random2DigitNumber(iLower, iUpper)
Randomize
Random2DigitNumber = _
Right("00" & Int((iUpper - iLower + 1) * Rnd + iLower), 2)
End Function
 
Back
Top