Read and change last symbols in text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
slavger
Posts: 4
Joined: 10 Feb 2017 09:27

Read and change last symbols in text file

#1 Post by slavger » 10 Feb 2017 10:00

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read and change last symbols in text file

#2 Post by Squashman » 10 Feb 2017 10:10

You are inside a code block so environmental variables need to be referenced with delayed expansion.

slavger
Posts: 4
Joined: 10 Feb 2017 09:27

Re: Read and change last symbols in text file

#3 Post by slavger » 11 Feb 2017 08:57

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Read and change last symbols in text file

#4 Post by aGerman » 11 Feb 2017 11:02

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Read and change last symbols in text file

#5 Post by Aacini » 11 Feb 2017 11:21

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

slavger
Posts: 4
Joined: 10 Feb 2017 09:27

Re: Read and change last symbols in text file

#6 Post by slavger » 13 Feb 2017 03:06

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read and change last symbols in text file

#7 Post by Squashman » 13 Feb 2017 07:40

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.

slavger
Posts: 4
Joined: 10 Feb 2017 09:27

Re: Read and change last symbols in text file

#8 Post by slavger » 14 Feb 2017 03:48

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.

Post Reply