Variable name stored in delayed variable - Batch - batch-file

I have:
setlocal EnableDelayedExpansion
var inLoopVar=hey
var first=!inLoopVar!
echo %!first!%
This outputs ECHO OFF because %!first!% is returned as empty.
How can I print "Hey"
Updated and Clarified:
Here is my full code with comment of what i am trying to do
#ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
set y=%%j
FOR /f "tokens=1-7 delims=; " %%a IN ("!y!") DO (
set aaa=%%a
set testVar=!aaa:~5!
REM basically testVar resolves to RESULT
echo !!testVar!!
REM Above echo prints "RESULT"
echo %!!testVar!!%
REM Above echo prints "ECHO is off."
)
)
set /a i=%i%+1
goto loop
Instead of ECHO is off. i am trying to output TRUE

#ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
CALL :sub %%j
)
set /a i=%i%+1
goto loop
GOTO :EOF
:sub
SET "namepart="
FOR %%a IN (%*) DO IF DEFINED namepart (
SET "Valuepart=!%%a!"
ECHO !namepart! is !valuepart!
SET "namepart="
) ELSE (
SET "namepart=%%a"
)
GOTO :eof
It would help if you would explain what you intend to do rather than set up an inquest to determine what you want to do by examining how you have failed to do whatever it is.

Related

Return value from a cmd function

I have a batch function and I need to return a value from it. Following is the script:
#echo off
call :Mode mode1
echo mode is %mode1%
:Mode
setlocal enabledelayedexpansion
set count=0
for /f "tokens=*" %%x in (map.txt) do (
set /a count+=1
set var[!count!]=%%x
)
for /f "tokens=2 delims=: " %%A in ("%var[2]%") Do (
set mode=OPEN
)
IF %mode%==OPEN (
echo coming into open
set %1=OPEN
echo %mode1%
) ELSE (
echo coming into shorted
set %1=SHORTED
echo %mode1%
)
EXIT /B 0
echo mode is %mode1% doesn't print anything. Any help? I've hardcoded set mode=OPEN for testing purposes.
Each exit, exit /b or goto :eof implicitly does an endlocal, so you need a trick for your variable %1 to survive an endlocal. endlocal & set ... & goto :eof does the trick because the whole line gets parsed in one go:
#echo off
call :Mode mode1
echo mode is %mode1%
goto :eof
:Mode
setlocal enabledelayedexpansion
set "mode=OPEN"
IF "%mode%" == "OPEN" (
echo coming into open
endlocal&set "%1=OPEN"&goto :eof
) ELSE (
echo coming into shorted
endlocal&set "%1=SHORTED"&goto :eof
)
For the same reason, echo %mode1% in your subroutine does not print the variable.
Note: I changed set and if syntax to recommended quoted syntax.

how to random array without duplicate using batch

Im using this batch for random test1-test3 without duplicate but not working
#echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
set /A "RND_TOTAL=3, FLAG_DUP=0"
set /A "RND_MIN=1, RND_MAX=3, RND_INTER=1"
for /L %%I in (1,1,%RND_TOTAL%) do (
call :SUB %%I
SET /A R=!RND_NUM[%%I]!
SET LINE[1]=TEST1
SET LINE[2]=TEST2
SET LINE[3]=TEST2
echo !LINE[%R%]!
pause
)
endlocal
exit /B
:SUB
set /A "RND_COUNT=%1-1"
:LOOP
set /A "RND_NUM[%1]=!RANDOM!%%((RND_MAX-RND_MIN)/RND_INTER+1)*RND_INTER+RND_MIN"
if %FLAG_DUP% EQU 0 (
for /L %%I in (1,1,%RND_COUNT%) do (
if !RND_NUM[%1]! EQU !RND_NUM[%%I]! (
goto :LOOP
)
)
)
exit /B
I get the following output:
echo off
echo off
echo off

print exact all characters from lines from one file to one file in batch file

Here i am printing line no 1 to 5 from a file to another file.its working fine but one small issue that the lines are trimming from left side,i do not want to trim,it should be same as the input file.
infile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
<EOC>/009</EOC>
<CTR>5</CTR>
code:
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
for /f "tokens=*" %%j in ( "%%i") do (
echo %%j
)
)
)
)
endlocal
exit /b 0
goto :eof
outfile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
The second loop is useless
It should work :
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
echo %%i
)
)
)
endlocal
exit /b 0
goto :eof
In your exemple, the spaces at left are not printed because they are considered as delimiter character
I would suggest this method:
#Echo Off
Set "Num=5"
<"infile.txt" (For /L %%A In (1,1,%Num%) Do (Set "_="
Set /P "_="
SetLocal EnableDelayedExpansion
Echo=!_!
EndLocal))>"outfile.txt"
Where you can adjust the value to Num as necessary.

Multiplying error: * was unexpected at this time

I recently got a problem. I am making a game module in batch and I got a strange error:
Multiplying error "*!Map_PlayerLengthX! was unexpected at this time"
This is the whole code:
#echo off
setlocal enabledelayedexpansion
pause >nul
cls
call :Map_DefinePlayer 4 2 loloolol
echo %Px1y1%%Px2y1%%Px3y1%%Px4y1%
echo %Px1y2%%Px2y2%%Px3y2%%Px4y2%
pause >nul
:::::::::::::::::::::::::::
:: Map v0.10 By KKZiomek ::
:::::::::::::::::::::::::::
:Map_Init
set Map_Running=1
goto :eof
:Map_Load
if !Map_Running!==0 goto :eof
set Map_Load_FileToLoad=%~1
set Map_Load_BorderX=%~2
set Map_Load_BorderY=%~3
set Map_Load_BChar=%~4
set Map_Load_LineTotal=0
for /f "tokens=* delims= usebackq" %%l in (!Map_Load_FileToLoad!) do (
set /a Map_Load_LineTotal+=1
set Map_Line!Map_Load_LineTotal!=%%l
)
:Map_Load_ApplyCoords
for /l %%g in (1,1,!Map_Load_LineTotal!) do (
call :Map_Load_StrLen Map_Line%%g Map_Line%%g_Len
set /a Map_Load_ApplyCoords_DecidedLen+=!Map_Line%%g_Len!
)
set /a Map_Load_ApplyCoords_DecidedLen/=!Map_Load_LineTotal!
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set x%%xy%%y=!Map_Line%%y:~%%x,1!
)
)
goto :eof
:Map_Load_StrLen
setlocal disabledelayedexpansion
set Map_Load_StrLen_Len=0
if defined %~1 for /f "delims=:" %%n in (
'"(cmd /v:on /c echo(!%~1!&echo()|findstr /o ^^"'
) do set /a "Map_Load_StrLen_Len=%%n-3"
endlocal & if "%~2" neq "" (set %~2=%Map_Load_StrLen_Len%) else echo %Map_Load_StrLen_Len%
exit /b
:Map_Display
cls
for /l %%y in (1,1,!Map_Load_LineTotal!) do (
set Map_Display_Line%%y=
for /l %%x in (1,1,!Map_Load_ApplyCoords_DecidedLen!) do (
set Map_Display_Line%%y=!Map_Display_Line%%y!!x%%xy%%y!
)
)
for /l %%z in (1,1,!Map_Load_LineTotal!) do (
echo !Map_Display_Line%%z!
)
goto :eof
:Map_ReloadPos
set XPos=%~1
set YPos=%~2
set x!XPos!y!YPos!=!Map_Line%YPos%:~%XPos%,1!
goto :eof
:Map_DefinePlayer
set Map_PlayerLengthX=%~1
set Map_PlayerWidthY=%~2
set Map_DefinePlayer_Scheme=%~3
set /a Map_DefinePlayer_Modifier=!Map_PlayerLengthX!-1
for /l %%p in (1,1,!Map_PlayerWidthY!) do (
for /l %%q in (0,1,!Map_DefinePlayer_Modifier!) do (
set /a localq=%%q+1
set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
set Px%localq%y%%p=!Map_DefinePlayer_Scheme:~%modq%,1!
)
)
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
:::::::::::::::::::::::::::
I get the error in the function :Map_DefinePlayer. I think it's mainly in this line: set /a modq=%%q+((%%p-1)*!Map_PlayerLengthX!)
Every function works fine instead of this function because of this weird multiplying error. I tried enbling delayed expansion again, changing !Map_PlayerLengthX! into %Map_PlayerLengthX% but then it only changed the error in *4 was unexpected at this time
Anyone has an idea what causes this and how to fix it?
try with :
set /a "modq=%%q+((%%p-1)*!Map_PlayerLengthX!)"
the brackets in the expression are taken as closing brackets for the FOR loop

( was unexpected at this time in a FOR loop

So Im having this problem where my batch for loop doesnt work, I havent found any solutions for this. It keeps saying "( was unexpected at this time". Could anyone help please?
for /F %%G IN ('TYPE info.txt') DO (
set /a cnt+=1
set /a div=%cnt% %% 2
if %div% == 0 {
set ord=%%G
echo %ord%
}
)
This only works if you use delayed expansion, otherwise all your variables will only be evaluated once.
Setlocal EnableDelayedExpansion
set cnt=0
for /F %%G IN ('TYPE info.txt') DO (
set /a cnt+=1
set /a div=!cnt! %% 2
if !div!==0 (
set ord=%%G
echo !ord!
)
)
Or you could use labels instead.
for /F %%G IN ('TYPE info.txt') DO call :line %%G
goto :EOF
:line
set /a cnt+=1
set /a div=%cnt% %% 2
if "%div%"=="0" call :div0 %1
goto :EOF
:div0
set ord=%1
echo %ord%
goto :EOF

Resources