Defining a changing variable - batch-file

I'm trying to create a variable that will link to others based off of a variable result, but can't seem to get it to work. Here's the code:
#echo off
set rollnumbera=10
set /a num=%random% %%rollnumbera +1
set "message=msg%num%"
set msg1=a
set msg2=b
set msg3=c
set msg4=d
set msg5=e
set msg6=f
set msg7=g
set msg8=h
set msg9=i
set msg10=j
echo %message%
echo %num%
pause
When I run it it displays the msg%num% result, but does not seem to be linking to the set msg#s. I'm wondering if there is a way to do that, or if an "if %num% == message number (echo message)" is required. This is just an ease of access question, but any advice would be appreciated.

The fixed code:
#echo off
setlocal enabledelayedexpansion
set rollnumbera=10
set /a num=(%random% %% rollnumbera)+1
set msg1=a
set msg2=b
set msg3=c
set msg4=d
set msg5=e
set msg6=f
set msg7=g
set msg8=h
set msg9=i
set msg10=j
for %%a in ("!msg%num%!") do set message=%%a
echo %message%
echo %num%
pause

Related

Using Mode Command to Resize Command prompt Clears Entire Screen

So I try Writing the codes to generate random characters and I want to make the screen always at the same size but when I tried using mode command it clears all text or you can call it entire screen
Here is the code:
:startprg
mode con: cols=120 lines=24
setlocal EnableDelayedExpansion
set char=abcdeijklmnstuvwxyzABCDETUVYZ23456789^^%%_`^^%%_`^^%%_`^^%%_`^^%%_`☺☻♥♦♣♠◘•○☺☻♥♦♣♠◘♂♀q7•-6○Z♂n¡* /*-+##$*()_=-=+
set count=0
set hla=0
:Number
set hla=120
set /a length=-1 + !hla!
:Loop
set /a count+=1
set /a rand=%Random%%%61
set buffer=!buffer!!char:~%rand%,1!
if !count! leq !length! goto Loop
title ^!buffer!
echo ^!buffer!
set /a length+=1
endlocal
goto startprg
Please help me I'm on Windows 10 and I don't know how to fix this problem
Currently each time you goto :startprg you reset the mode. Simply move the label to after mode con: in order to initiate it only once. So it should be:
#echo off
mode con: cols=120 lines=24
:startprg
setlocal EnableDelayedExpansion
So the full code would be as:
#echo off
mode con: cols=120 lines=24
:startprg
setlocal EnableDelayedExpansion
set char=abcdeijklmnstuvwxyzABCDETUVYZ23456789^^%%_`^^%%_`^^%%_`^^%%_`^^%%_`☺☻♥♦♣♠◘•○☺☻♥♦♣♠◘♂♀q7•-6○Z♂n¡* /*-+##$*()_=-=+
set count=0
set hla=0
:Number
set hla=120
set /a length=-1 + !hla!
:Loop
set /a count+=1
set /a rand=%Random%%%61
set buffer=!buffer!!char:~%rand%,1!
if !count! leq !length! goto Loop
title ^!buffer!
echo ^!buffer!
set /a length+=1
endlocal
goto startprg

Double variables in batch?(Or a way around them?)

I'm making a card game, so I'm trying to set a specific amount of times a number can be used, so I'm basically looking for either a way to do "double variables" or a way around them:
#echo off
color 0a
set 1=13
set 2=13
set 3=13
set 4=13
:loop
if %counter%==0
goto :skip
set /a card=%random%*4/32767+1
set %card%=%%card%%-1
goto :loop
:skip
....
I think this is the logic you are going for. Your assumption was correct. You do need double variable expansion. This can be achieved two ways. With Delayed Expansion or using the CALL command. I chose the latter. For the example I set the counter to 13 and kept echoing the contents of each of the variables after each loop.
#echo off
set _1=13
set _2=13
set _3=13
set _4=13
set "counter=13"
:loop
IF "%counter%"=="0" goto skip
set /a card=%random% %% 4 +1
call set /a _%card%=%%_%card%%% - 1
echo _1=%_1%
echo _2=%_2%
echo _3=%_3%
echo _4=%_4%
set /a counter-=1
goto :loop
:skip
pause
EDIT: Now that I look at that again, you actually don't need to use the CALL at all or delayed expansion because the SET command will take care of it for you.
#echo off
set _1=13
set _2=13
set _3=13
set _4=13
set "counter=13"
:loop
IF "%counter%"=="0" goto skip
set /a card=%random% %% 4 +1
set /a _%card%-=1
echo _1=%_1%
echo _2=%_2%
echo _3=%_3%
echo _4=%_4%
set /a counter-=1
goto :loop
:skip

How does this batch password generation work?

I am creating a password generator in batch, and had to use some help from the internet to finish it, but I am having difficulty interpreting the section of the code I had to borrow. Would someone please explain it?
:generate
#Echo Off
color 0a
set /P usernumberlength="What length do you want your password to be? "
pause
cls
Setlocal EnableDelayedExpansion
Set RNDLength=%usernumberlength%
/// from here
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set Str=%Alphanumeric%987654321
:loop
IF NOT "%Str:~18%"=="" SET Str=%Str:~9%& SET /A Length+=9& GOTO :loop
SET tmp=%Str:~9,1%
SET /A Length=Length+tmp
Set count=0
SET RndAlphaNum=
:loop2
Set /a count+=1
SET RND=%Random%
Set /A RND=RND%%%Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
If !count! lss %RNDLength% goto loop2
/// to here
Echo Password is: is !RndAlphaNum!
Echo Now choose what you want to do.
Echo 1) Go back to the beginning
Echo 2) Exit
set input=
set /p input= Choice:
if %input%==1 goto generate
if %input&==2 exit
pause
This working example (from the code you provided) contains the all-you-need section.
#echo Off
color 0a
setlocal enabledelayedexpansion
:generate
set /p usernumberlength="What length do you want your password to be? "
rem ----- begin section all you need -----
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
rem "Length" is the string length of %alphanumeric%
Set Length=62
set count=0
rem "RndAlphaNum" is the output
SET RndAlphaNum=
:loop
Set /a count+=1
Set /A RND=%Random% %% %Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
if !count! lss %usernumberlength% goto loop
rem ----- end section all you need -----
echo Password is: !RndAlphaNum!
goto generate

batch file blank variable

I'm trying to rename the contents of a folder based on it's name and adding an alphabet at the end. But I'm having a hard time getting the letter based on my defined array of alphabets. Here's my code so far.
#echo off
set letters[1]=a
set letters[2]=b
set letters[3]=c
set letters[4]=d
set letters[5]=e
set letters[6]=f
set letters[7]=g
set letters[8]=h
set letters[9]=i
set letters[10]=j
set letters[11]=k
set letters[12]=l
set letters[13]=m
set letters[14]=n
set letters[15]=o
set letters[16]=p
set letters[17]=q
set letters[18]=r
set letters[19]=s
set letters[20]=t
set letters[21]=u
set letters[22]=v
set letters[23]=w
set letters[24]=x
set letters[25]=y
set letters[26]=z
set /a index=0
pushd %1
for %%a in (%1) do set folder=%%~na
echo renaming %folder%...
setlocal enabledelayedexpansion
for %%i in (*.*) do (
set /a index+=1
set suffix=!letters[%index%]!
echo appending -!suffix!...
ren "%%~fi" "%folder%-!suffix!.*"
)
endlocal
popd
pause
exit /b
I have read that using the EnableDelayedExpansion helps in these kinds of situation also the use of (!) for the delayed variable? I'm still pretty new at this, I don't know where am I going wrong with this or how I can create a work around for this. Thanks!
Replace the line
set suffix=!letters[%index%]!
with
CALL set suffix=%%letters[!index!]%%
%index% refers to the value of index at the time the for %%i is parsed, not as it changes through the operation of the loop.
Note that you'll run out of suffixes if you have more than 26 targets!
(btw - for %%a in (%1) do set folder=%%~na is probably better-off as set folder=%%~n1 )

batch file variable problems

I am having problems with variables transferring throughout my batch file.
This is a rough example of what I have:
#echo off
setlocal enabledelayedexpansion
:one
set variableone=outputone
set variabletwo=outputtwo
set variablethree=outputthree
goto two
:two
set /a variable%variableone%four=numberone
set /a variable%variabletwo%five=numbertwo
set /a variable%variablethree%six=numberthree
goto three
:three
set /a variable%variableone%four+=(2*(!variable%variabletwo%five!-!!variable%variablethree%six!)
echo !variable%variableone%four!
exit
It is a lot longer than that and this is just a simplified version of what it actually is, however, the variables in the label ":three" won't transfer down so the variable ends up blank which leaves the equation blank as well. Is there a way to fix this?
It's tremendously difficult to see what you are doing.
In the below code, I've replaced the variable names NUMBER* with values.
I've also added the missing close-parenthesis and I'm wondering about the two successinve !
#ECHO OFF
setlocal enabledelayedexpansion
:one
set variableone=outputone
set variabletwo=outputtwo
set variablethree=outputthree
goto two
:two
set /a variable%variableone%four=numberone
set /a variable%variabletwo%five=numbertwo
set /a variable%variablethree%six=numberthree
set /a variable%variableone%four=14
set /a variable%variabletwo%five=25
set /a variable%variablethree%six=36
goto three
:three
set /a variable%variableone%four+=(2*(!variable%variabletwo%five!-!!variable%variablethree%six!))
echo !variable%variableone%four!
set var
Now for me, what is being echoed is -8 which is equal to 14+(2*(25-36))
So - works for me!

Resources