Page 1 of 1

Set specific lines of text as a %variable%

Posted: 25 May 2011 11:44
by dltaylor02
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.

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

Posted: 25 May 2011 12:29
by aGerman
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

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

Posted: 27 May 2011 10:59
by dltaylor02
Thank you so much! Worked like a charm. :lol:

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

Posted: 29 May 2011 01:23
by amel27
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"
)

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

Posted: 29 May 2011 08:25
by dbenham
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

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

Posted: 29 May 2011 11:17
by aGerman
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

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

Posted: 29 May 2011 15:38
by orange_batch
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.

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

Posted: 29 May 2011 19:59
by Ed Dyreen
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

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

Posted: 29 May 2011 21:15
by amel27
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

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

Posted: 29 May 2011 23:32
by Ed Dyreen
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 :!:

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

Posted: 29 May 2011 23:39
by dbenham
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

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

Posted: 29 May 2011 23:41
by Ed Dyreen
Hi BenHam :D
I knew it :lol:

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

Posted: 29 May 2011 23:53
by orange_batch
Thanks for the reassurance.

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

Posted: 30 May 2011 03:39
by amel27
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