0

I found a nice script that generates random numbers with javascript, without the drawbacks of using a command like ran =%random% in a batch file (it gives the same number for almost simultaneous calls). That code works but it prints the random number on screen. However, I dont know much of javascript and I cannot understand how I can call that java script function with 2 input variables and return the random value into another variable. I would like to do something like this

@set @e=0 /*
 @echo off
   set minV=0
   set maxV=5000
   set @e=
   cscript //nologo //e:jscript "%~f0" minV maxV
 exit /b
*/

function getRandomNumber(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

but it does not work. Also, I dont understand why the original script is using a "at" sign before "e" here

    set @e=

Is "e" a variable storing the result of the call?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Millemila
  • 1,612
  • 4
  • 24
  • 45
  • 2
    The first `@set @e=0` is a valid JScript assignment that is only used to insert the `/*` start of comment. The posterior `set @e=` command is just used to delete the `@e` variable in Batch; this line have not an useful result and may be removed... – Aacini Mar 29 '16 at 14:28

2 Answers2

1

If you want the Java function to remain unchanged, you could capture its output by a for /F loop:

@set @e=0 /*
  @echo off
    set minV=0
    set maxV=5000
    set @e=
    for /F "delims=" %%L in ('
      cscript //nologo //e:jscript "%~f0" %minV% %maxV%
    ') do set "ran=%%L"
  echo(%ran%
  pause
  exit /b
*/

function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

WScript.Echo(getRandomNumber(parseInt(WScript.Arguments(0)), parseInt(WScript.Arguments(1))));
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Thanks. Seems nice but when I copied in a .bat file and I run it, it seems not to update the value of ran. I added echo %ran% before exit /b but ran is empty. Any clue? – Millemila Mar 29 '16 at 15:09
  • Hmm, it works for me; I just added the `echo` plus a `pause` into the code to show the result even in case of double-clicking the `*.bat` file; did you place the `echo` at the same location? – aschipfl Mar 29 '16 at 15:51
  • oh sorry I missed to copy the last line of the code. I thought that WScript.Echo was only an echo of the javascript but its actually necessary to return a value to the batch file. However I obtain random numbers above 5000, they look between 0 and 50000 instead of between 0 to 5000. Is it the same for you? – Millemila Mar 29 '16 at 15:58
  • Sorry, I forgot to convert the `WScript.Argument`s to integers, so the result of `Math.floor()` and `min` were not added (`+`) but *appended* erroneously (the last digit was always `0` which is the value of `min`)... – aschipfl Mar 29 '16 at 16:33
  • Awesome now it works! So basically the "for /F" fully evaluate the string and assign the result to %%L? Cause its not that clear in the documentation. – Millemila Mar 30 '16 at 08:32
  • I actually do not understand what you mean by "fully evaluate the string"; you could go to [this site] and read the information there, because it is way more detailed than the help text of `for /?`... – aschipfl Mar 30 '16 at 09:06
  • thanks aschipfl but the site you provided is missing the link – Millemila Mar 30 '16 at 11:04
  • Basically I do not understand why I need a cycle, would not this be enough: set ran=cscript //nologo //e:jscript "%~f0" %minV% %maxV% – Millemila Mar 30 '16 at 11:05
  • Sorry... here is the link: http://ss64.com/nt/for.html. You cannot use `set var=command` to assign the command output to a variable; this would assign the literal string `command` to `var`... – aschipfl Mar 30 '16 at 11:39
0

Have this as last two lines in batch file.

If exist "%filterpath%FilterExit.bat" call "%filterpath%FilterExit.bat"
If exist "%filterpath%FilterExit.bat" del "%filterpath%FilterExit.bat"

and the equiv Jscript to this vbscript

Set Fso = CreateObject("Scripting.FileSystemObject")
Set File = Fso.CreateTextFile(FilterPath & "FilterExit.bat", True)
If err.number <> 0 then
    Outp.WriteLine "Error: " & err.number & " " & err.description & " from " & err.source
    err.clear
    wscript.exit
End If
File.WriteLine "set Filter_LineCount=" & Count
File.close

This runs a batch file from your existing batch file that your JScript creates.

PS If returning whole numbers you can use the wscript.quit <integer> and the integer will be in %errorlevel%.