Set specific lines of text as a %variable%
Moderator: DosItHelp
-
- Posts: 2
- Joined: 25 May 2011 11:40
Set specific lines of text as a %variable%
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.
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%
You could use a FOR /F loop and skip the first 2 lines.
Regards
aGerman
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
-
- Posts: 2
- Joined: 25 May 2011 11:40
Re: Set specific lines of text as a %variable%
Thank you so much! Worked like a charm.
Re: Set specific lines of text as a %variable%
I think, "If" is more preferably than "GoTo" (from reasons of speed):aGerman wrote:Code: Select all
for /f "usebackq skip=2 delims=" %%a in ("test.txt") do (set "valuefromtext=%%a" &goto exitFor)
:exitFor
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%
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%
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
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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Set specific lines of text as a %variable%
I personally use GOTO to BREAK. If only FOR could BREAK normally. 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%
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
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.
Re: Set specific lines of text as a %variable%
no, GOTO always scan whole BAT from start to first occurence of label (it is my reason)dbenham wrote:Plus the GOTO label is placed immediately after the loop so there is minimal time scanning for it.
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
Code: Select all
1
2
%i
var=1
Re: Set specific lines of text as a %variable%
I can't explain your result amel, but what about this then
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
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%
amel27 wrote:no, GOTO always scan whole BAT from start to first occurence of label (it is my reason)dbenham wrote:Plus the GOTO label is placed immediately after the loop so there is minimal time scanning for it.result: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 varCode: 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%
Hi BenHam
I knew it
I knew it
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Set specific lines of text as a %variable%
Thanks for the reassurance.
Re: Set specific lines of text as a %variable%
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.