Is it possible to do something like this?
set wordNum=1
set word=Test
:: ^
::This stuff is just so that if people want to
::test it, they have all the variables they need.
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
If so, what am I doing wrong?
In context, I'm trying to remind the computer of the last word it recorded.
Almost done.
setlocal enabledelayedexpansion
set wordNum=1
set word=Test
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
To use delayed expansion you need to enable it
Related
Is there any way to ask the user which variable wants to be shown and display it? It should be, as a concept, something like this:
#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo %%VAR_TO_SHOW%%
So if we are in "echo %%VAR_TO_SHOW%%" if we entered "VAR_1" I would want to look like this:
echo %VAR_1%
So the output would be "THIS IS VAR_1". Summarizing all what I've said, I would want to make a variable inside a variable. How can I make this?
Another thing I would want to comment is that I've alredy tried "%%!VAR!%%" and "%%VAR%%" without the exclamation marks, but what is displayied is or "%!VAR_TO_SHOW!%" or the same but without (again) the exclamation marks...
#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
call echo %%%VAR_TO_SHOW%%%
or
#echo off
setlocal enableDelayedExpansion
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo !%VAR_TO_SHOW%!
the second way will work faster.
I want to set a variable that uses another variable in its name. I want to do something like:
set a=2
set b=0
set s%a%%b%=Yee
Obviously, this doesn't work, but I want to be able to call the variable by doing:
echo %s20%
So it would echo Yee. This may be something you can't do, but it would make setting lots of variables much easier.
#echo off
set a=2
set b=0
set s%a%%b%=Yee
call echo %%s%a%%b%%%
:: OR ::
setlocal enableDelayedExpansion
echo !s%a%%b%!
endlocal
Better use the way with the delayed expansion as the call hits the performance.
I'm running this command and I don't see why it won't work
setlocal EnableDelayedExpansion
for %%a in (harry-boy) do set %%a:-==
echo %harry%
pause
And this is the result I get -
e:\6\1>setlocal EnableDelayedExpansion
e:\6\1>for %a in (harry-boy) do set %a:-==
e:\6\1>set harry-boy:-==
e:\6\1>echo
ECHO is on.
e:\6\1>pause
Press any key to continue . . .
I'm changing the hyphen sign to a equals sign then running the set command on that. I expect to see that the variable "harry" = "boy"??
Here is a simple test -
set file=play=here.mkv
set %file:==-%
echo %file%
pause
and I get this -
set file=play=here.mkv
==-%
was unexpected at this time.
set %file:==-%
I thought I would get the new contents of file = play-here.mkv. Ok, I see that this makes the syntax wrong and the set command stops. So how do I change the = to a hyphen?
The string replacement format:
%var:old-string=new-string%
does NOT work on for replaceable parameters, just in Batch variables. The equivalent way for your example, using a variable instead, would be:
set a=harry-boy
set %a:-==%
echo %harry%
pause
Output:
C:>set a=harry-boy
C:>set harry=boy
C:>echo boy
boy
C:>pause
Press any key to continue . . .
Easy soloution:
for /f "tokens=1,2 delims=-" %%a in ("harry-boy") do set %%a=%%b
Echo %harry%
And that should do your job for you. But it will only work with one - in the quote.
SET will assign the value on the right of the first = to an environment variable named on the left.
Hence you would be assigning a value of = to a variable named harry-boy:- in BOTH cases.
You can verify this by executing
set harr
which will display any variable starting harr
I am trying to get nested variables in my batch game i am creating.
I want it so that it will choose a random variable and change it to X, but if it is already chosen, it should go back and choose a different number.
set 1=a
set 2=b
set 3=c
set 4=d
set 5=e
those were the variables, here is the code
setlocal enabledelayedexpansion
:eliminator
set /a eliminate=(%random * 5) / 32767 + 1
if %%eliminate%%==X goto eliminator
echo The letter !!eliminate!! was chosen
timeout 5
set %%eliminate%%=X
goto eliminator
Now, the thing is, when I try to echo it, it writes the name of the variable instead of the value. Also, variables that have already been chosen are being chosen again. Any way I could fix this? Thanks.
try this:
#echo off&setlocal
set "var1=a"
set "var2=b"
set "var3=c"
set "var4=d"
set "var5=e"
:loop
set /a rd=%random%%%5+1
if defined var%rd% call echo %%var%rd%%%
set "var%rd%="
set "var" >nul 2>&1 && goto:loop
..output (may vary):
d
a
c
b
e
Your posted code is missing the closing % around random - it should read %random%.
Your formula for a random number between 1 and 5 is more complicated than need be. I would use:
set /a eliminate=%random% %% 5 + 1
To expand a "nested variable" you need !%eliminate%!
But I would completely rewrite your algorithm. I think the following does what you want:
#echo off
setlocal enableDelayedExpansion
set "chars=abcde"
set charCnt=5
:loop
set /a "pos=%random% %% charCnt, pos2=pos+1, charCnt-=1"
set "letter=!chars:~%pos%,1!"
echo The letter %letter% was chosen
set "chars=!chars:~0,%pos%!!chars:~%pos2%!"
if defined chars goto loop
The script is optimized to always pick a valid unused letter on each iteration.
This is part of the code for a game I'm making.
:south
set "message=You take a step South"
set /a "posY=%posY%+1" //Moves player down one tile
if "%p%posX%%posY%%"=="#" set /a "posY=%posY%-1" //checks if the player has hit a wall. If this is the case, bring him back one tile.
goto renderMap
Assuming %posX%==1 and %posY%==3 I'm trying to get the program to read the IF statement as:
if "p13"=="#" set /a "posY=%posY%-1"
But nothing I've tried seems to work. I was wondering if anyone could show me a proper way to do it.
#ECHO OFF
SETLOCAL
SET p13=#
SET posy=2
SET posx=1
SET /a posy=posy+1
CALL SET destsq=%%p%posx%%posy%%%
IF "%destsq%"=="#" (
ECHO hit wall - ouch!&SET /a posy-=1
) ELSE (ECHO moved south.)
This is probably easiest.
note that what is CALLed is
set destsq=
%% - % escapes the special meaning of %
p
%posx% - evaluated as 1
%posy% - evaluated as 3
%% - % escapes the special meaning of %
so the result is
set destq=%p13%
Note that set/a allows you to do operations without the % and also allows the form set /a var+=something to add %something% to var.
Of course,
set /a var += something
set /a var += %something%
set /a var = var + something
set /a var = %var% + something
set /a var = %var% + %something%
set /a var = var + %something%
all do precisely the same thing. Your choice about which style you use...
see
set /?
from the prompt for docco.
If you are writting a game in Batch, then the speed of your program is important.
There are three ways to use a variable as part of the name of another one: with CALL command, with FOR command, or using Delayed Expansion. CALL is the slowest one and Delayed Expansion the fastest.
Always try to use the shortest way to write any command. The fastest way to increment a variabe is set /A var+=1.
I strongly suggest you to use the standard array notation enclosing the subscripts in square braquets; this form is much clearer. You may read Arrays, linked lists and other data structures in cmd.exe (batch) script for further explanations on this point.
Below is your same code above, but including the previous points:
setlocal EnableDelayedExpansion
:south
set "message=You take a step South"
set /a posY+=1 //Moves player down one tile
if "!p[%posX%][%posY%]!" == "#" set /a posY-=1 //checks if the player has hit a wall. If this is the case, bring him back one tile.
goto renderMap
OK, here is an simple example where you can see, how it works:
#echo off&setlocal enabledelayedexpansion
set "posX=1"
set "posY=3"
set "p13=#"
set "pos=p%posX%%posY%"
if "!%pos%!"=="#" echo "#" found.
.. output is:
"#" found.