Need some help in getting this work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 4
Joined: 26 May 2021 09:07

Need some help in getting this work

#1 Post by Lordoa » 30 May 2021 05:07

I think what I'm trying to accomplish will be clear by looking at my code.

Code: Select all

@echo on
setlocal enabledelayedexpansion

:: The below is used to clear variables so that it's easier for my eyes when I debug by checking the "set" command. In other words useless.
for /f "tokens=1 delims==" %%a in ('set') do (
	if not "%%a" == "Path" set %%a=

title GetRandom

:: Main part
set getrandom="=="" for /l %%a in (1,1,2) do if "%%a" == "2" (for /f "tokens=1,2* delims=,[]" %%b in ("^^^!args^^^!") do (set /a getresult$=^^^!random^^^!*^^^(%%c-%%b+1^^^)/32768+%%b^& if "^^^!getresult$^^^!%%d)) else set args=
goto menu

:: Random
:: usage:
:: %getrandom%[min,max]

:: Some scribbles of me trying to get this to work.
:: echo "^^^!getresult$^^^!%%d
:: if "^!getresult$^!%%d

echo Goal: evaluate directly into if statement.
if "%getrandom%[1,5]" == "1" echo Success
rem set
goto menu
Calling the script using cmd /k will give you an even clearer picture.
So yes, I'm trying to hijack the if statement, below is how it works:

Code: Select all

if "%getrandom%[1,5]" == "1" echo Success
if ""=="" for /l ... set args=[1,5]" == "1" echo Success
Args are separated into: [1], [5], [" == "1" echo Success]
After computing the random value, it tries:
if "[random value][" == "1" echo Success]" -> if "2" == "1" echo Success
At least, that's the goal
Now, where it goes wrong.
if "^^^!getresult$^^^!%%d
causes it to end abruptly without an error message. Replacing it with, for example, a simple echo will work fine, but will not accomplish what I want. (try it for yourself)

As for why the macro is just a gigantic one-liner, well it's easier to understand (since I'm not familiar with <LF>)

I narrowed down the problem to the double quotes ["] in the above. Echo:ing it works fine without them, but the if-statement doesn't.
Removing the quotes from the if-comparison in the code still works fine with echo, but if-statement gives an error: ") was unexpected at this time"

Code: Select all

In getrandom:
if ^^^!getresult$^^^!%%d

if "%getrandom[1,5] == 1 echo Success

) was unexpected at this time
Brackets are balanced afaik

I now think I know what's wrong.
It somehow checks the if-statement syntax beforehand (before it expands).
Because of the nature of the problem the solution is probably to do more parsing on the third argument and separate the operator.
I attempted to do trick around a bit, but this did not work:

Code: Select all

for /f "delims=" %%e in ("^^^!getresult$^^^!") do if %%e%%d^^^&rem == rem echo nul
I assume it's still trying to do the original comparison with the equal signs. (at rem == rem)
I attempted to have %%rem equ %%rem expand into "rem" later, but this didn't work either.

Conclusion, unsolvable, needs a workaround, where:
"solution is probably to do more parsing on the third argument and separate the operator."
Then check the operator using an if command, after which it should look like
if ^^^!getresult$^^^! == %%d [stuff]

Thank you,

Post Reply