0

Here is my code:

for %%i in ("joined/*.mp4") do (
  set /a result=(%random%*2/32768)+1
  echo %result%
)

It gives me errors about +1 was unexpected at this time.

I tried another variant:

for %%i in ("joined/*.mp4") do (
  set /a result=(%random%*2/32768)
  echo %result%
)

It gives me an error about unbalanced parenthesis.

How can I echo the random variable correctly?

Thanks. :)

Trying the following code gives me the same value of random every time. How can I change it with each itertion of the loop?

setlocal EnableDelayedExpansion
for %%i in ("joined/*.mp4") do (
  set /a result= %random%*20/32768 + 1
  echo !result!
)

Is there a resource that I can read to learn in detail how batch files work and their language like loops, arrays etc.? I tried searching on Google but nothing useful came up.

Real Noob
  • 1,369
  • 2
  • 15
  • 29
  • 2
    remove the parentheses in the `set /a` statement. You don't need them and the closing `)` closes your `for` loop too early. And you should read about [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) (for both `random` and `result`) – Stephan Mar 11 '21 at 09:49
  • Thanks @Stephan. :) The code works now but the value of `result` seems to stay the same throughout the loop. – Real Noob Mar 11 '21 at 09:59
  • I figured it out, I need to use `!` everywhere. :) – Real Noob Mar 11 '21 at 10:03

2 Answers2

1

As explained in the link provided by @Stephan, you'll need the ! on random too.

You can do it like this (for some reason, (!random!*20)/32768 triggers "/32768" was unexepected, so you have to split it on two lines :

setlocal EnableDelayedExpansion
    for %%i in ("joined/*.mp4") do (
        set /a result = !random!*20
        set /a result= !result!/32768 +1
        echo !result!
    )
XouDo
  • 945
  • 10
  • 19
  • Thanks @XouDo. :) However, I am unable to use the value of !result! in other commands like `ffmpeg -i "joined/%%~ni.mp4" -i music/!result!.mp3` within the batch file. It shows me an error `music/!result!.mp3: No such file or directory`. – Real Noob Mar 11 '21 at 11:42
  • Maybe there's an issue with your paths : you should be using backslash ` \ ` (windows style path) instead of the slash `/` (unix style). – XouDo Mar 11 '21 at 12:10
  • Thanks @XouDo but that did not resolve the error. I will try something else. :) – Real Noob Mar 11 '21 at 13:31
  • 1
    Ok @RealNoob, let us know when you'll finally solve your problem. Please note that once outside of the `FOR` loop, you can use `%result%` back again instead of `!result!`. – XouDo Mar 11 '21 at 17:55
1

Using brackets, (parentheses), is perfectly fine, and I would say more correct, so there's no reason why you cannot continue to do so.

Hare some options for you, which maintain their use:

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
    Set /A result = (!random! * 20 / 32768^) + 1
    Echo !result!
)
Pause
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
    Set /A "result = (!random! * 20 / 32768) + 1"
    Echo !result!
)
Pause
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
    Set /A result = (!random! %% 20^) + 1
    Echo !result!
)
Pause
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
    Set /A "result = (!random! %% 20) + 1"
    Echo !result!
)
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks @Compo. The problem now is that I am unable to use the value of `!result!` anywhere. It just stays as literal `!result!`. Example, `ffmpeg -i "joined/%%~ni.mp4" -i music/!result!.mp3`. – Real Noob Mar 11 '21 at 12:32
  • I have answered your asked question, including the same commands you had used @RealNoob, but with the required corrections. The rest appears to be a completely different problem, which is outside of the scope of the question you asked. _You have also already been told that Windows uses the backward slash as its path separator, not the forward slash, (as used in unix based systems). I clearly already made that correction for you within the `For` parentheses._ – Compo Mar 11 '21 at 12:54
  • Thanks @Compo but changing the slashes did not resolve the error. :) I will try something else. – Real Noob Mar 11 '21 at 13:29
  • I never said it would @RealNoob, I just corrected your error. I cannot assist you with a problem I cannot replicate. By providing a random command, not used in your question, and with no context whatsover, you cannot expect me to assist you with an issue it exhibits. – Compo Mar 11 '21 at 13:32
  • Hi @Compo Should I update the question with a full set of commands then? – Real Noob Mar 11 '21 at 13:37
  • No @RealNoob, you should submit a new question, using the new code, provided here, and adapted with your actual commands. _You should also, before you do that, check the usage information for your 'different' commands to ensure that they use the correct options and syntax, before doing so_. – Compo Mar 11 '21 at 14:43