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.

3
  • 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 (for both random and result) Commented Mar 11, 2021 at 9:49
  • Thanks @Stephan. :) The code works now but the value of result seems to stay the same throughout the loop. Commented Mar 11, 2021 at 9:59
  • I figured it out, I need to use ! everywhere. :) Commented Mar 11, 2021 at 10:03

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

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.
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.
Thanks @Compo but changing the slashes did not resolve the error. :) I will try something else.
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.
Hi @Compo Should I update the question with a full set of commands then?
|
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!
    )

4 Comments

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.
Maybe there's an issue with your paths : you should be using backslash ` \ ` (windows style path) instead of the slash / (unix style).
Thanks @XouDo but that did not resolve the error. I will try something else. :)
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!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.