0

I have written this script to test passing a variable to another batch file and display the output coming back from this called batch. Here is the code I am using:

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /L %%v in (0,1 15) do (
FOR /F "tokens=*" %%i IN ('tohex.bat %%v') DO set h=%%i
echo %h%
)

as output I am getting 16 lines of "F"s, as if I have run tohex 15

Yet, if I take the loop part out and run this script with the value of variable v set to a single number manually, as seen below:

@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set v=13   && rem here I tried with different values assigned to v
@FOR /F "tokens=*" %%i IN ('tohex.bat %%v') DO set h=%%i
echo %h%

I am getting the correct hexadecimal digit as the response setlocal line leving in or taking out, doesn't make any difference how my batch runs.

And here is the tohex script which I copied from some other resource on the web:

REM TOHEX.BAT
@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set LOOKUP=0123456789abcdef &set HEXSTR=&set PREFIX=
if "%1"=="" echo 0&goto :EOF
set /a A=%* || exit /b 1
if !A! LSS 0 set /a A=0xfffffff + !A! + 1 & set PREFIX=f
:loop
set /a B=!A! %% 16 & set /a A=!A! / 16
set HEXSTR=!LOOKUP:~%B%,1!%HEXSTR%
if %A% GTR 0 goto :loop
echo %PREFIX%%HEXSTR%
goto :EOF

I am unable to make any sense out of this. I tried introducing a delay into every iteration of the loop to observe no change in the outcome.

Any help/pointers are greatly appreciated.

MelBurslan
  • 2,289
  • 2
  • 17
  • 25
  • 1
    You have to use delayed expansion (see the duplicate): `echo !h!`, like `TOHEX.BAT` does. (By the way: you don't need this variable, you can just `for .... do echo %%i` (btw: when you `set v=13` then, of course, you have to use `%v%` (or `!v!`) instead of `%%v`) – Stephan Jun 14 '21 at 21:30

0 Answers0