DosTips.com

A Forum all about DOS Batch
It is currently 25 Jun 2017 04:26

All times are UTC-06:00




Post new topic  Reply to topic  [ 8 posts ] 
Author Message
PostPosted: 10 Feb 2017 10:00 
Offline

Joined: 10 Feb 2017 09:27
Posts: 4
Hi guys, I'm trying to change some characters in txt file, but It's too difficult for my mind :(

I have a text file with some data and I need to replace last hex numbers in each string - result should be = 0x5b - 0x02 (for first string respectively)
Code: Select all
10.36.215.1  01 01 02 e0 36 76 d6 9f 5b
10.36.215.2  01 01 02 e0 36 76 d6 a2 4d
10.36.215.3  01 01 02 e0 36 76 d6 a5 99
10.36.215.4  01 01 02 e0 36 76 d6 a6 13
10.36.215.5  01 01 02 e0 36 76 d6 9c cd
10.36.215.6  01 01 02 e0 36 76 d6 a5 ff
10.36.215.7  01 01 02 e0 36 76 d6 a4 7f
10.36.215.8  01 01 02 e0 36 76 d6 a2 17
10.36.215.9  01 01 02 e0 36 76 d6 a3 7f
10.36.215.10  01 01 02 e0 36 76 d6 a2 61
10.36.215.11  01 01 02 e0 36 76 d6 a2 83
10.36.215.12  01 01 02 e0 36 76 d6 a4 a3
10.36.215.13  01 01 02 e0 36 76 d6 a1 e7
10.36.215.14  01 01 02 e0 36 76 d6 a3 fd



so I try to do like this
Code: Select all
FOR /F "tokens=1,5-10" %%a IN (tmp1.txt) do (
 @echo  %%a wrong WWN: 1000%%b%%c%%d%%e%%f%%g
 @echo  %WRITE2SW1%>>runme.txt
 set hex=0x%%g
 set /a varh=hex - 0x2
 call :dec2hex %varh% results

 @echo  %%a correct WWN: 1000%%b%%c%%d%%e%%f%results%
)

but it doesn't work - set hex=0x%%g, where %%g last nubers in string. :?
Pls help me to fix my mistake.

Thank you.


Top
   
PostPosted: 10 Feb 2017 10:10 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3507
You are inside a code block so environmental variables need to be referenced with delayed expansion.


Top
   
PostPosted: 11 Feb 2017 08:57 
Offline

Joined: 10 Feb 2017 09:27
Posts: 4
Squashman wrote:
You are inside a code block so environmental variables need to be referenced with delayed expansion.

Thanks, a lot. Can you share a simple explanation, of course if you have some free times? :oops:
Anyway thanks!


Top
   
PostPosted: 11 Feb 2017 11:02 
Online
Expert

Joined: 22 Jan 2010 18:01
Posts: 2672
Location: Germany
Variables in a command line or in a block of command lines enclosed into parentheses (such as the body of your FOR loop) will be expanded to their values only once. That is, before the command line or block is executed. In order to bypass this early expansion you have to enable the delayed variable expansion in a sub environment using
Code: Select all
setlocal EnableDelayedExpansion

Surrounding percent signs of variables have to be replaced with surrounding exclamation marks.

not working:
Code: Select all
@echo off

set "x=12345"
echo before: %x%

for /l %%i in (1 1 3) do (
  set "x=%%i"
  echo %x%
)

echo after: %x%

pause


working:
Code: Select all
@echo off

setlocal EnableDelayedExpansion
set "x=12345"
echo before: %x%

for /l %%i in (1 1 3) do (
  set "x=%%i"
  echo !x!
)

echo after: %x%

pause


Steffen


Top
   
PostPosted: 11 Feb 2017 11:21 
Offline
Expert

Joined: 06 Dec 2011 22:15
Posts: 1375
Location: México City, México
I like this example:

Code: Select all
@echo off
setlocal EnableDelayedExpansion

set "var=Old"
set "var=New" & echo Normal: "%var%"  Delayed: "!var!"


rem To exchange the values of two variables:
set "var1=%var2%" & set "var2=%var1%"

Antonio


Top
   
PostPosted: 13 Feb 2017 03:06 
Offline

Joined: 10 Feb 2017 09:27
Posts: 4
Thanks guys. :!:

It works, but I find another issue - can't call a function from FOR loop.

Code: Select all
@echo off
setlocal enabledelayedexpansion

set a=5B
set /a xx=0x%a% -0x2
call :dec2hex %xx% result
echo DEC result %xx%
echo HEX result %result%

for /l %%i in ( 1 1 3) do (
set "var1=0x5%%i"
set /a var2=var1 - 0x02
echo result in DEC: !var2!
call :dec2hex %var2% results
echo result in HEX: !results!
)


in results I can't make DEC 2 HEX conversion.

Code: Select all
DEC result 89 
HEX result 59
result in DEC: 79
result in HEX:
result in DEC: 80
result in HEX:
result in DEC: 81
result in HEX:


I'm sorry guys, maybe I need some help againe.
Thanks.


Top
   
PostPosted: 13 Feb 2017 07:40 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3507
slavger wrote:
Thanks guys. :!:

It works, but I find another issue - can't call a function from FOR loop.

Code: Select all
@echo off
setlocal enabledelayedexpansion

set a=5B
set /a xx=0x%a% -0x2
call :dec2hex %xx% result
echo DEC result %xx%
echo HEX result %result%

for /l %%i in ( 1 1 3) do (
set "var1=0x5%%i"
set /a var2=var1 - 0x02
echo result in DEC: !var2!
call :dec2hex %var2% results
echo result in HEX: !results!
)


in results I can't make DEC 2 HEX conversion.

Code: Select all
DEC result 89 
HEX result 59
result in DEC: 79
result in HEX:
result in DEC: 80
result in HEX:
result in DEC: 81
result in HEX:


I'm sorry guys, maybe I need some help againe.
Thanks.

So you still do not understand delayed expansion inside a code block which is what we tried to explain to you already.


Top
   
PostPosted: 14 Feb 2017 03:48 
Offline

Joined: 10 Feb 2017 09:27
Posts: 4
Squashman wrote:
So you still do not understand delayed expansion inside a code block which is what we tried to explain to you already.


Already finished my script. :roll:

Thank you for your patience.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 8 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: aGerman and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited