Set specific lines of text as a %variable%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dltaylor02
Posts: 2
Joined: 25 May 2011 11:40

Set specific lines of text as a %variable%

#1 Post by dltaylor02 » 25 May 2011 11:44

Hi, i'm really really new to writing batch files. I only now understand most of the syntaxing for the set command and havent even started learning the for command yet.

Anyways, I need to set the third line of a text file as a variable to be used in the command set /a value=(value1-valuefromtext)

Your help is Hugely appreciated and i thank in you advance.

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

Re: Set specific lines of text as a %variable%

#2 Post by aGerman » 25 May 2011 12:29

You could use a FOR /F loop and skip the first 2 lines.

Code: Select all

for /f "usebackq skip=2 delims=" %%a in ("test.txt") do (set "valuefromtext=%%a" &goto exitFor)
:exitFor
set /a value=value1-valuefromtext

Regards
aGerman

dltaylor02
Posts: 2
Joined: 25 May 2011 11:40

Re: Set specific lines of text as a %variable%

#3 Post by dltaylor02 » 27 May 2011 10:59

Thank you so much! Worked like a charm. :lol:

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Set specific lines of text as a %variable%

#4 Post by amel27 » 29 May 2011 01:23

aGerman wrote:

Code: Select all

for /f "usebackq skip=2 delims=" %%a in ("test.txt") do (set "valuefromtext=%%a" &goto exitFor)
:exitFor
I think, "If" is more preferably than "GoTo" (from reasons of speed):

Code: Select all

set "valuefromtext="
for /f "usebackq skip=2 delims=" %%a in ("test.txt") do (
  if not defined valuefromtext set "valuefromtext=%%a"
)

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

Re: Set specific lines of text as a %variable%

#5 Post by dbenham » 29 May 2011 08:25

amel27 wrote:I think, "If" is more preferably than "GoTo" (from reasons of speed):

Actually in my experience the GOTO is slightly faster then IF when logically trying to "exit a loop early". A GOTO within the loop seems to short circuit the body of the loop so it no longer is executed. Plus the GOTO label is placed immediately after the loop so there is minimal time scanning for it. The odd thing is the loop iterator itself continues until completion. Of course the IF must execute each iteration.

I'm not sure what really is going on, I'm just inferring from timing experiments.

For me the difference in time between the IF and GOTO is negligible - just 2.5%

Dave Benham

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

Re: Set specific lines of text as a %variable%

#6 Post by aGerman » 29 May 2011 11:17

Of course GOTO is no good programming. But there is no better way to abort a FOR loop in batch.
amel27's code loops over the entire file, and how much time it will waste depends on how many lines the file contain.

Regards
aGerman

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Set specific lines of text as a %variable%

#7 Post by orange_batch » 29 May 2011 15:38

I personally use GOTO to BREAK. If only FOR could BREAK normally. 8) But GOTO also allows pseudo-"while" loops. Useful for numerous conditions and when FOR loops would become too complex.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Set specific lines of text as a %variable%

#8 Post by Ed Dyreen » 29 May 2011 19:59

Yes well, with the timerdiff macro we could tell the difference in speed

But I think goto is best if you look at it like this:
set "?=" &for /l %%! in ( 1, 1, 999 ) do if not defined ? set "?=defined"
set "?=" &for /l %%! in ( 1, 1, 999 ) do if not defined ? set "?=defined " &goto :skippy the kangeroo
:skippy the kangeroo
set "?=" &for /l %%! in ( 1, 1, 999 ) do set "?=defined " &goto :skippy the kangeroo
:skippy the kangeroo
Last edited by Ed Dyreen on 29 May 2011 23:28, edited 1 time in total.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Set specific lines of text as a %variable%

#9 Post by amel27 » 29 May 2011 21:15

dbenham wrote:Plus the GOTO label is placed immediately after the loop so there is minimal time scanning for it.
no, GOTO always scan whole BAT from start to first occurence of label (it is my reason)

Code: Select all

@echo off

for /l %%i in (1,1,10) do (
 if %%i equ 3 set "var=1" & goto:BREAK
:BREAK
 echo %%i
)
set var
result:

Code: Select all

1
2
%i
var=1

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Set specific lines of text as a %variable%

#10 Post by Ed Dyreen » 29 May 2011 23:32

I can't explain your result amel, but what about this then

Code: Select all

@echo off
goto :skip
echo.1
:skip
echo.2
goto :skip
echo.3
goto :skip
:skip
echo.see
pause
exit /b

Code: Select all

2
see
Druk op een toets om door te gaan. . .


see it starts from the next line to the end and then from the beginning until it reaches it's starting point at which point you'll get an error :!:

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

Re: Set specific lines of text as a %variable%

#11 Post by dbenham » 29 May 2011 23:39

amel27 wrote:
dbenham wrote:Plus the GOTO label is placed immediately after the loop so there is minimal time scanning for it.
no, GOTO always scan whole BAT from start to first occurence of label (it is my reason)

Code: Select all

@echo off

for /l %%i in (1,1,10) do (
 if %%i equ 3 set "var=1" & goto:BREAK
:BREAK
 echo %%i
)
set var
result:

Code: Select all

1
2
%i
var=1

I have read many times that GOTO begins scanning from the current location until it reaches the end of file, then resumes scanning from the top. In your code the batch file has already read past the :BREAK label, so it is a worst case scenario - it has to read virtually the entire file to find the label. But if you move the :BREAK to the line following the loop ending ) it should find the label immediately.

Plus I have done timing of both options (IF vs. GOTO within loop) and the GOTO is slightly faster.

Finally, this should put GOTO scan process matter to rest:

Code: Select all


@echo off
goto :start

:break
echo GOTO starts scan from the top
exit /b

:start
for /l %%n in (1,1,100) do goto :break

:break
echo GOTO scans from current location before looping to top
exit /b

Output:

Code: Select all

GOTO scans from current location before looping to top


Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Set specific lines of text as a %variable%

#12 Post by Ed Dyreen » 29 May 2011 23:41

Hi BenHam :D
I knew it :lol:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Set specific lines of text as a %variable%

#13 Post by orange_batch » 29 May 2011 23:53

Thanks for the reassurance.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Set specific lines of text as a %variable%

#14 Post by amel27 » 30 May 2011 03:39

dbenham wrote:I have read many times that GOTO begins scanning from the current location until it reaches the end of file, then resumes scanning from the top.

Nice thread. Thanks, Dave.
I am glad to appear wrong. :D

Post Reply