set /p command does not work in loop

June 18, 2009 at 05:55:34
Specs: Windows XP

I am writing a batch file that convert decimal value to hex. I have a main program calling the subroutines that do the calculation. The subroutines output the conversion to a temp.dat. My main program will output the temp.dat to a variable. But when I m doing this in a loop, the command line set /p hexvar=<temp.dat doesn't work. The hexvar was always empty. Any suggestion how to get round this and why this probblem occurs?

The following is my code:

FOR /L %%N IN (9,1,12) DO (
call :convert_dec2hex_4byte %%N
set /p hexvar=<temp.dat
ECHO overlay %hexvar%.......


:: Enable delayed variable expansion

echo argument=%1

:: Use PING to split the number into 4 2-digit hexadecimal numbers that
:: are displayed again as 4 decimal numbers from 0 to 255 (FOD or Four
:: Octet Decimal)
FOR /F "tokens=2 delims= " %%A IN ('PING %1 -n 1 -w 1 2^>NUL') DO IF "!FOD!"=="" SET FOD=%%A
FOR /F "tokens=1-4 delims=." %%A IN ('ECHO.%FOD%') DO (
IF "%%~D"=="" (
) ELSE (
CALL :D2H TmpA %%A
CALL :D2H TmpB %%B
CALL :D2H TmpC %%C
CALL :D2H TmpD %%D

:: Display the end result
ECHO 0x%TmpA%%TmpB%%TmpC%%TmpD% > temp.dat

:: Done


:: Split the number (0-255) into 2 single "digits"
SET /A Tmp2 = %2 / 16
SET /A Tmp1 = %2 - 16 * %Tmp2%
:: Convert decimal 0-15 to single digit hexadecimal
SET Cvt=0123456789ABCDEF
SET Tmp1=!Cvt:~%Tmp1%,1!
SET Tmp2=!Cvt:~%Tmp2%,1!
:: Concatenate the 2 hexadecimal digits
SET %1=%Tmp2%%Tmp1%


See More: set /p command does not work in loop

Report •

June 18, 2009 at 06:56:17
if you can afford to download GNU tools and Gawk (see my sig)
here's a simpler solution
C:\test>echo 255|gawk "{printf \"%x\", $0}"
C:\test>echo 10|gawk "{printf \"%x\", $0}"

GNU win32 packages | Gawk

Report •

June 18, 2009 at 07:58:45
At first glance, my guess is you're getting snagged by the setlocal/endlocal.

If at first you don't succeed, you're about average.


Report •

June 18, 2009 at 08:56:04
Your main lacks to enable the delayed expansion as you can't set and then refer a variable inside the same block sequence (a set of statements embraced by ()),

At the end of your main should be coded a GOTO :EOF before the subroutine label. More as M2 pointed out it seems you do not have a clear understand of the use of setlocal/endlocal directives, but I leave to you (or another guy) to go further on this issue as now I have to attend to other business.

setlocal EnableDelayedExpansion
FOR /L %%N IN (9,1,12) DO (
call :convert_dec2hex_4byte %%N
set /p hexvar=<temp.dat
ECHO overlay !hexvar!......

Report •

Related Solutions

June 18, 2009 at 15:33:51
  @echo off & setlocal enableextensions
  set decimal_=%1
  call :DecimalToHex %decimal_% hex_
  echo Decimal %decimal_% = Hexadecimal %hex_%
  endlocal & goto :EOF

  :DecimalToHex DecimalIn HexOut
  setlocal enableextensions
  set /a dec_=%1
  if %dec_% LSS 0 (endlocal & set %2=Error: negative & goto :EOF)
  set hex_=
    set /a dec1_=%dec_%
    set /a dec_=%dec_% / 16
    set /a hexdigit_=%dec1_% - 16*%dec_%
    set /a hexidx_=%hexdigit_%+1
    for /f "tokens=%hexidx_%" %%h in (
      'echo 0 1 2 3 4 5 6 7 8 9 A B C D E F') do set hexdigit_=%%h
    set hex_=%hexdigit_%%hex_%
    if %dec_% GTR 0 goto :_hexloop
  endlocal & set %2=%hex_% & goto :EOF

Report •

Ask Question