Hello guys I am very bad at notepad, I want to make a random code generator.
That Generates codes like this 3K2EU-ZGS5L-P3DNL-YM9JC
I want it to work with a notepad .bat file so I can let it run on my PC
This can be done using %random% values to assign numbers and letters as demonstrated
below. If you need letters and numbers in specific places within the 20 digits,
adjust the Condition used to call getLetters or Getnumber to call according to current
%dig%==number
#ECHO OFF
SETLOCAL enableDelayedExpansion
:open
Set dig=0
:main
Set /a dig=%dig% + 1
CALL :digit%dig%
Call LetorNum
IF %dig%==20 goto result
GOTO main
:LetorNum
Set /a pick=%random% * 2 / 32768 + 1
IF %pick%==1 call :getLetter
IF %pick%==2 call :getNumber
GOTO :EOF
:getLetter
Set /a Letter=%random% * 26 / 32768 + 1
IF %letter%==1 Set disp!dig!=a
REM repeat for each letter value between a and z
IF %letter%==26 Set disp!dig!=z
GOTO :EOF
:getNumber
Set /a number=%random% * 10 / 32768 + 1
IF %number%==1 Set disp!dig!=0
REM repeat for every number value between zero and 9
IF %number%==9 Set disp!dig!=9
GOTO :EOF
:result
ECHO %disp1%%disp2%%disp3%%disp4%%disp5%-%disp6%%disp7%%disp8%%disp9%%disp10%-
%disp11%%disp12%%disp13%%disp14%%disp15%-%disp16%%disp17%%disp18%%disp19%%disp20%
pause >nul
GOTO open
Related
So I've been trying to make the following code:
set /a num1=10
set /a num2=%random% %%60 +%num1%
echo %num2%
(This is simplified)
For this code I need the +%num1% to be a variable because I need to be able to change the lowest number.
For some reason, instead of giving me a random number it gives a totally unrelated number, that isn't random either, but the same every time. My first thought was it was perhaps adding the second variable instead of making a randomizer. That is not the case though, and I'm not sure how to fix this issue.
I have also tried the following code:
set /a num1=10
set /a num2=(%random%*60/32768)+%num1%
echo %num2%
The issue with this code is it never seems to work as randomizer for me even without the variable.
Any help is appreciated.
Here's some examples to assist you:
Set "num1=10"
Set /A "num2 = (%RANDOM% %% 60) + num1"
Echo(%num2%
Set "num1=10"
Set /A "num2 = (%RANDOM% * 60 / 32768) + num1"
Echo(%num2%
Please note, that we have only been provided with a very small portion of your batch file, so if this code is part of a parenthesized block, you may need to enable delayed expansion and use !RANDOM! and possibly !num2! instead of %RANDOM% and %num2% respectively.
In the first form you are have to account for modulus bias, as described further in this question Why do people say there is modulo bias when using a random number generator?
(Note: the accepted answer there has a flaw which I pointed out and provide a solution to in my answer to that question.)
In both the first and the second form they present an incorrect range of values the way it is written. More info can be found here How to use random in BATCH script?
Long story Short:
In both forms, if you want your range to start at 10 and go to 60 you will need to adjust it to be using a modulus of 50, in the first form you must account for modulus bias in your calculations causing some numbers to appear more often.
The first form can be fixed using this method:
SET "Min=10"
SET "Max=60"
SET /A "Discard= 32768 - ( ( ( 32768 %% (Min-Max) ) + 1 ) %% (Min-Max) )"
:Rand
SET /A "num2=%random%"
IF %num2% GTR %Discard% GOTO :Rand
SET /A "num2= num2 %% (Min-Max) + Min"
echo=%num2%
The second form can be fixed using this method:
SET "Min=10"
SET "Max=60"
SET /A "num2= %random% * ( Max - Min + 1 ) / 32768 + Min "
echo=%num2%
Example of Full script:
#(SETLOCAL
ECHO OFF
)
CALL :Main
( ENDLOCAL
EXIT /B
pause
)
:Main
SET "Min=10"
SET "Max=60"
SET /A "Discard= 32768 - ( ( ( 32768 %% (Min-Max) ) + 1 ) %% (Min-Max) )"
ECHO=Form1:
CALL :Form1
ECHO=Form2:
CALL :Form2
pause
GOTO :EOF
:Form1
SET "num2=%random%"
IF %num2% GTR %Discard% GOTO :Form1
SET /A "num2= num2 %% ( Min - Max ) + Min"
echo=%num2%
GOTO :EOF
:Form2
SET /A "num2= %random% * ( Max - Min + 1 ) / 32768 + Min "
echo=%num2%
GOTO :EOF
Results:
C:\WINDOWS\system32>C:\Admin\SE\testrandom.cmd
Form1:
10
Form2:
31
Press any key to continue . . .
C:\WINDOWS\system32>
Following is the code in which I want to create 10 files if the counter reach 10 but the comparison is not working am I missing something or am I doing something wrong? It creates only one file and prints as following in that one file
10 == 0 set
#echo off
set limit=10
set count=0
:start
set count = %count% + 1
echo %limit% == %count% set > YouAreAnIdiot%random%.txt
if %count%==%limit%
exit 0
else
goto start
two errors in one line: set count = %count% + 1:
a) the space between count and = is part of your variable name. (It would be %count %)
b) to calculate with set, you need the /a parameter:
set /a count=%count% + 1
Surprisingly, set /a doesn't care for the additional space, but get used to the syntax without spaces around the = - this keeps life simple.
set /a doesn't need the percent signs with variables, so set /a count=count+1 also works.
There is a short form to do that:
set /a count+=1
Also your if statement will not work. The complete construct has to be on one (logical) line:
if %count%==%limit% (
exit 0
) else (
goto start
)
(note the spaces around the parantheses - they are critical)
When I use this code to select a character from my list, it works just fine but when I write it to a file using:
echo %pwd%>>pwd.gen
It will some times put the word "ECHO" randomly in the middle of the strings generated. Here is an example:
jUrkunjcxC
ecRECHOsI5w0T
DmJfat13fT
UWXOysW7Gb
pPmS7138Ve
nFkh32ECHOJd1
You can see it appears in line 2 and 6. This only happens about 20% of the time.
Here is the code:
#echo off
title Password Generator
color f0
:firstRun
set /a cnt=0
cls
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set /p Len=What length should the password be?
set /a Len=%Len%-1
cls
set /p Amt=How many would you like to generate?
cls
goto start
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
echo %pwd%>> pwd.gen
set /a cnt=%cnt%+1
if %cnt%==%Amt% goto end
goto start
:end
cls
echo Done!
echo Results have been saved to "pwd.gen"
echo.
choice /c YN /m "Restart?"
if %errorlevel%==1 goto firstRun
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
alfanum is 26+26+10 = 62 characters long.
RANDOM gives a random number from 0-32,767
When RANDOM is above 32240, rnd_num gets set to 62
string indexing starts at 0 not 1
the for /F %%c command indexes alfanum:~62,1~ which is an empty string
it calls echo with no parameter, which prints ECHO is on. instead of returning a single character
for /F defaults to splitting strings with a space delimiter, which separates out the first word
%%c becomes ECHO
you add ECHO into the password.
This is a combination of a couple of things. While I'm not totally clear about the inner workings of the whole thing, I know what's causing it and how to fix it.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 is 62 characters long. However, substrings in batch start with 0, so valid numbers go from 0 to 61. For whatever reason, an index-out-of-range combined with the ^ in 'echo %%alfanum:~!rnd_num!^,1%%' is causing the word ECHO to be displayed.
To get around this, simply don't add 1 when calculating rnd_num.
SET /A rnd_num=!RANDOM! * 62 / 32768
I am making a small script that would assign roles as an app sort of thing. It asks for the number of people, then it runs random to see what role you are.
Like this:
:mrole
set/a player1=%random% * 4 / 32768 + 1
if %player1%==1 set 1role=Murderer
But what if I only want a certain ratio of people to get a certain role? Say 1:4 are murderers. If I do something like this for up to 8 players what if 6 of them are murderers? What if I want to run through again for a Detective role if there are 8 people? The current one I have for mrole is 29 lines! Is there a quicker way than checking if there are too many EVERY time? And the Detective check is going to be very long too!
What I have so far is below and here's a link: http://pastebin.com/g2GfN7v9
#echo off
:new
set test=2
:main
cls
echo Trouble in Murder Town
echo --------------------------
echo.
echo 1. Start
echo 2. Tester Options
echo 3. Quit
echo.
set/p m=
if %m%==1 goto start
if %m%==2 goto testrat
if %m%==3 exit
goto main
:testrat
cls
echo Chance to have a tester
echo -----------------------
echo Currently a 1 in %test% chance to have a tester in a game
echo.
echo 1. Always
echo 2. Never
echo 3. 1/2 (default)
echo 4. 1/3
echo 5. 1/4
echo 6. 1/5
echo 7. Back
echo.
set/p t=
if %t%==1 set test=1
if %t%==2 set test=0
if %t%==3 set test=2
if %t%==4 set test=3
if %t%==5 set test=4
if %t%==6 set test=5
if %t%==7 goto options
goto testrat
:start
cls
echo How many players not including the one at the computer?
echo.
echo 1. 3
echo 2. 4
echo 3. 5
echo 4. 6
echo 5. 7
echo 6. 8
echo 7. Quit
echo.
set/p t=
if %t%==1 set players=3
if %t%==2 set players=4
if %t%==3 set players=5
if %t%==4 set players=6
if %t%==5 set players=7
if %t%==6 set players=8
if %t%==1 goto names
if %t%==2 goto names
if %t%==3 goto names
if %t%==4 goto names
if %t%==5 goto names
if %t%==6 goto names
if %t%==7 goto main
goto start
:names
cls
echo Please enter their names.
echo.
set/p player1=Player 1:
echo.
set/p player2=Player 2:
echo.
set/p player3=Player 3:
echo.
if %players%==8 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
echo.
set/p player7=Player 7:
echo.
set/p player8=Player 8:
goto role
) else if %players%==7 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
echo.
set/p player7=Player 7:
goto role
) else if %players%==6 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
echo.
set/p player6=Player 6:
goto role
) else if %players%==5 (
set/p player4=Player 4:
echo.
set/p player5=Player 5:
goto role
) else if %players%==4 (
set/p player4=Player 4:
goto role
) else goto role
:role
set 1role=0
set 2role=0
set 3role=0
set 4role=0
set 5role=0
set 6role=0
set 7role=0
set 8role=0
set murderers=0
set detectives=0
goto mrole
:mrole
set/a player1=%random% * 4 / 32768 + 1
if %player1%==1 set 1role=Murderer
if %player1%==1 set/a murderers=%murderers% + 1
if %player1%==1 set mcheck=y
if %player1% neq 1 set mcheck=n
if %mcheck%==y if %players% lss 8 goto drole
set/a player2=%random% * 4 / 32768 + 1
if %murderers%==1 if %players%==8 if %player2%==1 set 2role=Murderer
if %mcheck%==y if %players% lss 8 goto drole
set/a player3=%random% * 4 / 32768 + 1
if %murderers%==1 if %players%==8 if %player3%==1 set 3role=Murderer
if %mcheck%==y if %players% lss 8 goto drole
set/a player4=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player4%==1 set 4role=Murderer
set/a player5=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player5%==1 set 5role=Murderer
set/a player6=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player6%==1 set 6role=Murderer
set/a player7=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player7%==1 set 7role=Murderer
set/a player8=%random% * 4 / 32768 + 1
if %mcheck%==y if %players% lss 8 goto drole
if %murderers%==1 if %players%==8 if %player8%==1 set 8role=Murderer
if %murderers% lss 1 goto mrole
if %players%==8 if %murderers% lss 2 goto mrole
:drole
if %players%==8 (
) else if %players lss 8 goto irole
:irole
if 1role==0 set 1role=Innocent
if 2role==0 set 2role=Innocent
if 3role==0 set 3role=Innocent
if 4role==0 set 4role=Innocent
if 5role==0 set 5role=Innocent
if 6role==0 set 6role=Innocent
if 7role==0 set 7role=Innocent
if 8role==0 set 8role=Innocent
The algorithm you're looking for is a shuffle. Think of your roles as a deck
of cards, with one role per card. If you were playing a game with people, you'd
shuffle the deck of roles and hand one card to each person.
Study the following code that declares a deck of 4 cards and then shuffles them.
I've named the deck in the style of an array from more traditional programming
languages, but it's really just a convention. I could have named it in the
form of DECK.1, DECK.2, ... or whatever.
I've also isolated parts of the program into subroutines. There's a subroutine
to generate a random number, print an "array", shuffle an array, and swap 2
items in an array. When working with array-style indirection in batch, you'll
hit a wall if you don't break things into subroutines.
#echo off
setlocal EnableDelayedExpansion
set "DECK[0]=1H"
set "DECK[1]=2S"
set "DECK[2]=3C"
set "DECK[3]=4D"
set "DECK_SIZE=4"
call :print_array "DECK" %DECK_SIZE%
call :shuffle "DECK" %DECK_SIZE%
echo -----
call :print_array "DECK" %DECK_SIZE%
goto :eof
REM Generate random number from %1 min through %2 max.
:rand
set /a RAND_NUM=%RANDOM% * (%~2 - %~1 + 1) / 32768 + %~1
goto :eof
REM %1 Name of array to print
REM %2 Size of the array
:print_array
set /a LAST_INDEX=%~2-1
for /L %%a in (0, 1, %LAST_INDEX%) do (
echo %~1[%%a]: !%~1[%%a]!
)
goto :eof
rem To shuffle an array a of n elements (indices 0..n-1):
rem for i from n − 1 downto 1 do
rem j ← random integer with 0 ≤ j ≤ i
rem exchange a[j] and a[i]
REM %1 Name of array to shuffle
REM %2 Size of the array
:shuffle
set /a LAST_INDEX=%~2-1
for /L %%a in (0, 1, %LAST_INDEX%) do (
call :rand %%a %LAST_INDEX%
call :swap "%~1[%%a]" "%~1[!RAND_NUM!]"
)
goto :eof
REM %1 One of the variables to swap.
REM %2 The other variable to swap.
:swap
set "__tmp=!%~1!"
set "%~1=!%~2!"
set "%~2=!__tmp!"
goto :eof
So to modify this code for your program, you'd want an array of players and an
array of roles. The player array remains fixed, and you shuffle the role array.
E.g.,
set "PLAYER[0]=Howard"
set "PLAYER[1]=Fred"
set "PLAYER[2]=Robin"
set "PLAYER[3]=Gary"
set PLAYER_SIZE=4
set "ROLE[0]=murderer"
set "ROLE[1]=innocent"
set "ROLE[2]=innocent"
set "ROLE[3]=detective"
set "ROLE_SIZE=4"
call :shuffle "ROLE" %ROLE_SIZE%
set /a ROLE_LAST_INDEX=%ROLE_SIZE%-1
for /L %%a in (0, 1, %ROLE_LAST_INDEX%) do (
echo Player !PLAYER[%%a]! is !ROLE[%%a]!.
)
One sample output from this code would give you:
Player Howard is detective.
Player Fred is innocent.
Player Robin is murderer.
Player Gary is innocent.
#ECHO OFF
SETLOCAL
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET "roles=1234"
SET players=4
:setplayrole
SET /a player=1+(%RANDOM% %% %players%)
IF DEFINED $%player% GOTO setplayrole
SET $%player%=%roles:~-1%&SET "roles=%roles:~0,-1%"
IF DEFINED roles GOTO setplayrole
SET $
GOTO :EOF
Here's a snippet that might achieve your aim.
But first, a little commercial - you have tis line in your posting:
if %player1%==1 set 1role=Murderer
Um - it's possible to do this, but a really, really bad idea because of batch's syntax. Never start a variablename with a numeric. It has huge consequences, and none of the are good.
--- back to our - er, program...
I'm not clear whether your difficulty is assigning the ratios of roles to the players, but it seems that you want a proportion in each role; guaranteed there are no 'no-one playing this role'.
To me, what you'd do is assign a set of roles - I've just use 1..4 with an even distribution. You may want to have 10 players, one each in roles 1..4 and the others random, where you could build the roles string by appending the random roles to a seed of required roles.
Having established the set of roles for the game, you then assign each role in the string to a player, and remove the role as it is assigned.
I use a standard clear out all variables with names starting "$"' routine; then assign the required roles, (append any extras as you wish) then the loop is simple - select a random player; if that player already has an assigned role, select again. If not, simply assign the last role in the list and remove the last role entry. Continue until the roles list is empty, and all done...
I need to do a floating-point division in a dos batch.
I didn't find a way to do it. Something like this :
SET /A Res=10/3
returns a integer number.
Is it possible to do it ?
I know this is a very old topic, but I can't found a simple Batch method in all previous answers, so I post here a pure Batch solution that is very simple to use.
Perform operations using fixed point arithmetic in Batch is simple. "Fixed point" means that you must set a number of decimals in advance and keep it throughout the operations. Add and subtract operations between two Fixed Point numbers are performed directly. Multiply and division operations requires an auxiliary variable, that we can call "one", with the value of 1 with the right number of decimals (as "0" digits). After multiply, divide the product by "one"; before division, multiply the dividend by "one". Here it is:
#echo off
setlocal EnableDelayedExpansion
set decimals=2
set /A one=1, decimalsP1=decimals+1
for /L %%i in (1,1,%decimals%) do set "one=!one!0"
:getNumber
set /P "numA=Enter a number with %decimals% decimals: "
if "!numA:~-%decimalsP1%,1!" equ "." goto numOK
echo The number must have a point and %decimals% decimals
goto getNumber
:numOK
set numB=2.54
set "fpA=%numA:.=%"
set "fpB=%numB:.=%"
set /A add=fpA+fpB, sub=fpA-fpB, mul=fpA*fpB/one, div=fpA*one/fpB
echo %numA% + %numB% = !add:~0,-%decimals%!.!add:~-%decimals%!
echo %numA% - %numB% = !sub:~0,-%decimals%!.!sub:~-%decimals%!
echo %numA% * %numB% = !mul:~0,-%decimals%!.!mul:~-%decimals%!
echo %numA% / %numB% = !div:~0,-%decimals%!.!div:~-%decimals%!
For example:
Enter a number with 2 decimals: 3.76
3.76 + 2.54 = 6.30
3.76 - 2.54 = 1.22
3.76 * 2.54 = 9.55
3.76 / 2.54 = 1.48
Batch files as such do not support the floating point arithmetic. However, this article suggests a workaround that uses an external script file to do calculations. The script file should use some sort of eval function to evaluate the expression passed as an argument and return the result. Here's a sample VBScript file (eval.vbs) that does this:
WScript.Echo Eval(WScript.Arguments(0))
You can call this external script from your batch file, specify the expression to be evaluated and get the result back. For example:
#echo off
for /f %%n in ('cscript //nologo eval.vbs "10/3"') do (
set res=%%n
)
echo %res%
Of course, you'll get the result as a string, but it's better than nothing anyway, and you can pass the obtained result to the eval script as part of another expression.
According to this reference, there is no floating point type in DOS batch language:
Although variables do exist in the DOS batch programming language, they are extremely limited. There are no integer, pointer or floating point variable types, only strings.
I think what you are trying to do will be impossible without implementing your own division scheme to calculate the remainder explicitly.
I recently came across this batch file to compute an approximation of Pi.
There is a DivideByInteger label that might be useful to you: Stupid-Coding-Tricks-A-Batch-of-Pi
It uses a set of MaxQuadIndex variables, each containing a four-digit number (quadruple), in order to store the entire result. The code allows division by an integer between 1 and 10000, inclusive.
:DivideByInteger
if defined PiDebug echo.DivideByInteger %1 %2
set /a DBI_Carry = 0
for /L %%i in (!MaxQuadIndex!, -1, 0) do (
set /a DBI_Digit = DBI_Carry*10000 + %1_%%i
set /a DBI_Carry = DBI_Digit %% %2
set /a %1_%%i = DBI_Digit / %2
)
goto :EOF
A Print label is also available…
try this
SETLOCAL EnableExtensions EnableDelayedExpansion
call :calc_ 1 (99-(100*5/100^)^)
echo !calc_v!
goto :EOF
:calc_
set scale_=1
set calc_v=
for /l %%i in (1,1,%1) do set /a scale_*=10
set /a "calc_v=!scale_!*%2"
set /a calc_v1=!calc_v!/!scale_!
set /a calc_v2=!calc_v!-!calc_v1!*!scale_!
set calc_v=!calc_v1!.!calc_v2!
goto :EOF
just change
call :calc_ decimalpoint equataion
in the example
decimalpoint is 1
equataion is (99-(100*5/100^)^) ;make sure if you use () that you insert ^ before ) as in ^)
the answer is 94.0
if decimalpoint is 2
and equataion is 22/7 ;π pi
the answer is 3.14
I wrote a pure batch file specifically to do division. It takes the first number you input, and then divides it by the second one, and displays the result with as many decimal points as you specify.
Echo off
cls
if NOT "%3" == "" (
set n1=%1
set n2=%2
set max=%3
goto :begin
)
set counter=2
set n1=1
set n2=1
set ans=
:start
Echo.
Echo. 1 / 2
Echo.
Set /p N1= 1?
set /p N2= 2?
Set /p Max= Out how many Decimal Points?
:begin
set /a TmpAns=%N1%/%N2%
set ans=%TmpAns%.
:: Echo.%ans%.>Answer.txt
<nul set /p "=%Tmpans%."
set /a TmpSub=%N2%*%TmpAns%
set /a N1=%N1%-%TmpSub%
set N1=%N1%0
If NOT "%n1%" == "00" (
if %n1% LSS %N2% (
set N1=%N1%0
set ans=%ans%0
)
) else (
Goto :Finished
)
set count=0
:loop
If "%count%" == "%max%" (
Goto :Finished
)
set /a TmpAns=%N1%/%N2%
set ans=%ans%%TmpAns%
<nul set /p "=%Tmpans%"
set /a TmpSub=%N2%*%TmpAns%
set /a N1=%N1%-%TmpSub%
set N1=%N1%0
If NOT "%n1%" == "00" (
if %n1% LSS %N2% (
set N1=%N1%0
set ans=%ans%0
)
) else (
Goto :Finished
)
set /a count=%count%+1
goto :loop
:finished
cls
Echo.
Echo.
Echo.The Number
Echo.%ans%
Echo.
Echo.
set n1=1
set n2=1
pause
goto :eof
:eof
The answer put into the variable %Ans%. It can also be called with parameters. ("Divide.bat 50 27 5" would give you 50/27 out 5 decimal points.)
Since nowadays PowerShell is present on almost all machines, I would let PowerShell do the math and return the result to the batch.
Example:
set divident=10
set divisor=3
for /f "delims=" %%a in ('powershell -Command %divident%/%divisor%') do set result=%%a
#echo %result%
Explanation:
Input variables: Use set variables to define divident and divisor.
Calling powershell and assign result to a batch variable: for /f "delims=" %%a in ('powershell -Command ...) do set result=%%a (you may also check here: How to put a single PowerShell output string into a cmd variable?)
Note the above code will only work with integer input variables.
To support floating point input variables, we need to send the variables as strings inside quotations ("%variable%") and convert the strings within PowerShell back to Double, otherwise batch would interpret the commas as delimiters and PowerShell could not interpret the numbers.
Example:
set divident=10,5
set divisor=3,4
for /f "delims=" %%a in ('powershell -Command [convert]::ToDouble^(\"%divident%\"^)
/[convert]::ToDouble^(\"%divisor%\"^)') do set result=%%a
#echo %result%
Explanation:
Note in PowerShell you would do this like [convert]::ToDouble("10,5")/[convert]::ToDouble("3,5"). However in batch we need to escape the quotes using backslash, and we also need to add a "^" sign before and after the quoted parts: [convert]::ToDouble^("%divident%"^)/[convert]::ToDouble^("%divisor%"^)
If you're running in a command shell on Windows (rather than DOS), you can use VBScript to evaluate complex expressions including floating point math for you.
I have written a small helper library you can call to do this.
EvalBat Library on GitHub