1.

Solve : Mysterious Batch Error?

Answer»

I'm working on a batch file game (a sort of quiz).
The way it works is that there is a file called FACTS.txt which contains each item in the quiz in 6-line "blocks." Each "block" uses the following STRUCTURE:

  • Question
  • Choice a
  • Choice b
  • Choice c
  • Answer
  • Explanation

What I'm doing at the moment is the part that reads the first four lines of the file.
I'm using 'functions' (info at HTTP://ss64.com/nt/syntax-functions.html) and I have a function to read the first four lines. Here it is:
Code: [Select]:getquestion
setlocal EnableDelayedExpansion>>OUTPUT.txt
setlocal>>OUTPUT.txt
set number=%1%>>OUTPUT.txt
set /a startline=%number%*6>>OUTPUT.txt
set /a endline=%startline%+3>>OUTPUT.txt
set i=%startline%>>OUTPUT.txt
for /f "eol=¬" "skip=%startline%" %%a (FACTS.txt) DO (
set /a i+=1>>OUTPUT.txt>>OUTPUT.txt
echo %%a>>OUTPUT.txt
if %i%==%endline% do endlocal>>OUTPUT.txt
goto eof>>OUTPUT.txt
)
endlocal>>OUTPUT.txt
setlocal DisableDelayedExpansion>>OUTPUT.txt
goto :eof>>OUTPUT.txt
As you can see, I've been trying to figure out what the problem is by putting >>OUTPUT.txt at the end of each line to try to figure out what the problem is.
I run the batch, but the moment that function is called, it just closes.
I look in OUTPUT.txt. Nothing at all.

Can anybody track down the problem?

By the way, the line of code that calls the function is:
Code: [Select]call :getquestion %level%>>OUTPUT.txtYou would be better off having each question in your FACTS.txt file on a single line.  A heck of a lot easier to parse with a batch file that way.

Question, Choice A, Choice B, Choice C, Answer, Explanation.


Why do you have that GOTO EOF in your FOR LOOP.  That will immediately break the FOR LOOP after it parse the first line of the FACTS.txt file.  I believe the line should be GOTO :EOFIs the variable NUMBER the Question Number you are trying to pull from the Fact.txt file.  If so, your math logic is screwed up with that.  If you are passing the question number to the function you would need to multiply and then subtract to get the first line of the question.

Question 2 should be lines 7 to 12 of your text file.  But using your logic the startline would be 12 and the endline would be 15.
So to get your startline you should be doing this:
set /a startline=(%number%*6)-5In this line, where is the value of %1% coming from?
Quote
set number=%1%>>OUTPUT.txt
Quote from: Salmon TROUT on December 22, 2011, 10:01:22 AM
In this line, where is the value of %1% coming from?
I am assuming he is not showing us the entire batch file.  I am just assuming he is calling the function above and passing the question number with the CALL.Just remember that assuming makes an *censored* out of u and some guy named MING... Quote from: Squashman on December 22, 2011, 10:03:32 AM
I am just assuming he is calling the function above and passing the question number with the CALL.

If that is so, maybe he thinks that %1% is going to hold the passed parameter?

Quote from: BC_Programmer on December 22, 2011, 10:12:43 AM
Just remember that assuming makes an *censored* out of u and some guy named Ming...

I know that guy...he was in my Darts League...Ok. I suggest making a new output.txt file.






Discussion

No Comment Found