Reputation: 596
I have a batch file that I want to call subroutines within, and have them return to me an output string.
I've tried all kinds of combinations of setlocal
and endlocal
, different variations on % characters, and different methods: nothing has worked. I have scoured the web, but I don't think I'm searching the right things. Here is an attempt to set a global variable and change the contents pointed to by a reference, but I can't get the syntax, or maybe there's a better way. Here's my code:
echo off
for %%x in (0, 1, 15) do (
call :hex %%x RET
set str1=%RET%
for /l %%y in (0, 1, 15) do (
call :hex %%y RET
set str2=%RET%
echo %str1%%str2%
)
)
goto :eof
:hex
set _hex=0123456789ABCDEF
set /A len=1
set /A offset=%1
CALL SET s=%%_hex:~%offset%,%len%%%
set %2=%s%
set RET=%s%
goto :eof
I expect the output to look like incrementing HEX numbers, but it just prints out FF's.
What is going on? How does this syntax work? How can I call a label like a c function, and have it return a value?
Upvotes: 2
Views: 1338
Reputation: 57252
@echo off
setlocal enableDelayedExpansion
set _hex=0123456789ABCDEF
for /l %%x in (==0;1;15hex==) do (
for /f "tokens=1,2" %%A in ("%%x 1") do set "str1=!_hex:~%%A,%%B!"
for /l %%y in (;;0==1iterator==15;;) do (
for /f "tokens=1,2" %%A in ("%%y 1") do set "str2=!_hex:~%%A,%%B!"
echo !str1!!str2!
)
)
endlocal
goto :eof
the same logic , just removed the CALL
which only will make your script slower (for further reading - http://ss64.org/viewtopic.php?id=1669 ; http://ss64.org/viewtopic.php?id=1667).
Check also the delayed expansion -> http://ss64.com/nt/delayedexpansion.html
Upvotes: 1