|
Answer» I have a script which is comparing 2 files like this:
Code: [Select]:step1 for /f "tokens=* delims=" %%J in ('type file1.txt') do set line=%%J & call :step2
:step2 find "%line:~0,-1%" < file2.TXT >> output.txt my PROBLEM is that I want the script to continue with another call if the line is found, OTHERWISE I want the script to check the next line. Code: [Select] :step2 find "%line:~0,-1%" < file2.TXT >> output.txt if errorcode0 call :step3
:step3 blah blah blah blah blah blah
But I'm having a hard time returning step2 to step1.
can anyone give me a hand?Have you tried goto?
EG: Code: [Select]:step1 for /f "tokens=* delims=" %%J in ('type file1.txt') do set line=%%J & goto step2
:step2 find "%line:~0,-1%" < file2.TXT >> output.txt i thought that goto was basically only used for EOF. what's the difference between goto and call in this situation?Goto is used for LABELS and EOFs as you said.
Call is used for calling seperate batch files.i changed the call to goto but that's still not returning to the for command@mac , he cant use goto here
@grant
try this: Code: [Select]:step1 for /f "tokens=* delims=" %%J in ('type file1.txt') do set line=%%J & call :step2
:step2 find "%line:~0,-1%" < file2.TXT >> output.txt exit /b
or
Code: [Select]:step1 for /f "tokens=* delims=" %%J in ('type file1.txt') do set line=%%J & call :step2
:step2 find "%line:~0,-1%" < file2.TXT >> output.txt goto :EOFbasically this is how mac said, its caling another batch file, so in this case we can use goto :EOF (end of file) or exit /b which exits called script My fault forgot you can't use Gotos in For loops
And thanks for the help, Dev, never knew there were seperate parameters to Exitso at this point, it's:
Code: [Select]:step1 for /f "tokens=* delims=" %%J in ('type file1.TXT') do set line=%%J & call :step2
:step2 find "%line:~0,-1%" < file2.TXT >> todo.txt if errorcode0 call :step3 exit /b :step3 blah blah blah
and the script went through the 1st line and finished the script instead of going to the 2nd line.
also, why can't i use goto here?Since Goto is NESTED inside For, it can only be used with labels that are also inside that same for loop.
This also applies to variables in real-world programming, if you have a variable nested in a Function or Sub-routine, it's called a local variable which can only be used inside that function or sub-routine. Quote from: macdad- on May 28, 2009, 09:11:15 AM Since Goto is nested inside For, it can only be used with labels that are also inside that same for loop. goto: nested inside for loop usually used to break a loop when certain condition is met.
when to use call or goto: - use call when you want to goto a label and return to the next line after call when it reached a goto:eof statement (exception last line of code) - use goto when you want to goto a label without returning the control to the next line after goto - call statement is also use to run another batch
a label can accept argument supplied by both goto or call statement %1 - %9, %* to access allthat's great GUYS! thanks a lot.
|