hex values

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

hex values

#1 Post by jwoegerbauer » 01 Jan 2013 12:17

Hi all, new here

I could create a txt file that contains 3 lines as shown (without the REM)
REM
REM ---------- VIEWBIN.TXT
REM Image Start = 0x80100000, length = 0x01EDAD34

My problem is, how to extract the hex values (0x80100000 ...) using DOS, not sure
whether this would be correct

SET start =""
SET length =""
FOR /F "tokens=*" %%a IN ('type startlength.txt') DO
(
SET line=%%a
IF %line% = "---------- VIEWBIN.TXT" GOTO :NEXTLINE
SET start=%line:~14,10%
SET length=%line:~35,10%
:NEXTLINE
)

Thanks for helping.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: hex values

#2 Post by dbenham » 01 Jan 2013 14:08

There are problems with your code:

1) You cannot GOTO a label within a FOR loop - that immediately terminates the loop (it stops iterating)
2) You cannot set variable line and then access the value using %line% within a parenthesized block. You should use delayed expansion to do that.

There is no need to TYPE the file, you can simply use FOR /F to read the file directly. I'm assuming you want the numeric values.

If you know that you want the 3rd line, then you can use the following:

Code: Select all

@echo off
set "start="
set "length="
for /f "skip=2 tokens=2,4 delims=,=" %%A in (startlength.txt) do (
   set /a start=%%A
   set /a length=%%B
   goto :break
)
:break

If you want to preserve the hex notation, then remove the /a option from the set statements. This will also preserve leading and trailing spaces that you might not want. You could use another FOR /F loop to trim the spaces.

Code: Select all

   for /f %%N in ("%%A") do set "start=%%N"
   for /f %%N in ("%%B") do set "length=%%N"


If the location of the line may vary, you can use FINDSTR to find the correct line

Code: Select all

@echo off
set "start="
set "length="
for /f "skip=2 tokens=2,4 delims=,=" %%A in ('findstr /ic:"Image Start *=.*,length *=" startlength.txt) do (
   set /a start=%%A
   set /a length=%%B
)

You can adjust the search string as needed to make it as specific as is needed.


Dave Benham

Post Reply