Counter in batch file

February 5, 2010 at 15:15:29
Specs: Windows XP
Hi all, i'm making batch quiz at the moment, but i wanted to ask is it possible to add a counter in my batch quiz. E.g. if i answer first question right it sais 1/10 points and when i finishish quiz it calculates good answers and sais you answer e.g. 7/10 questions. So is it possible to make that in batch?

See More: Counter in batch file

Report •

#1
February 5, 2010 at 15:23:18
Its a trick question. Tipically NO because the Set command will not allow you to increment in a for loop. Now, the real answer is yes if you call your incrementation code from a kinda sub routine.

This will not work...

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

but if you do this...

    set LIST=
    for %i in (*) do call :setvar %i
    echo %LIST%
    goto :eof

    :setvar
    set LIST=!LIST! %1
    goto :eof

then it will. I think the answer they are looking for on the quiz might be NO but this would be one where I would ask the teacher to through the question out.

P.S. you might want to post this in the programming forum. You find this out my doing a set /? in a command prompt.

Oh, wait I read you question more closely, did you make your quiz using Batch Scripting? If so then just post your code (minus the question) and we can see what we can do.


Report •

#2
February 5, 2010 at 15:43:42
i dont know that i can post the code, because i made it in my language, and that language isn' t english, so all of you couldn't read that, but if it isn't no way to solve that problem i will make in english. by the way thanks for a little help

Report •

#3
February 5, 2010 at 15:57:11
You can you just have to put it in a subroutine like I posted in my example.

Report •

Related Solutions

#4
February 6, 2010 at 03:32:32
well ok, here is the code:
@echo off
:menu
cls
color 0f
echo ===============================================
echo.
echo Klausimelis
echo.
echo ===============================================
echo.
echo.
echo Sveiki atvyke i batch klausimeli!
echo.
echo.
:choise
echo 1 Pradeti klausimus
echo 2 Pagalba
echo.
set /p idk= Irasykite pasirinkima:
if %idk%== 1 goto vkl
if %idk%== 2 goto pagalba

:vkl
cls
echo Pirmasis klausimas:
echo.
echo Kas atrado Amerika?
echo.
echo 1) Vikingai
echo 2) K. Kolumbas
echo 3) Norvegai
echo 4) Magelanas
echo.
set /p vkl= Atsakyma irasykite cia:
if %vkl%== 2 goto dkl
if not %vkl%== 2 goto blogai

:dkl
cls
echo Antrasis klausimas:
echo.
echo Iguana yra..
echo.
echo 1) Zinduolis
echo 2) Reptilija
echo 3) Zuvis
echo 4) Paukstis
echo.
set /p dkl= Atsakyma irasykite cia:
if %dkl%== 2 goto tkl
if not %dkl%== 2 goto blogai

:tkl
cls
echo Treciasis klausimas
echo.
echo 2010 metai yra...
echo.
echo 1) Jaucio metai
echo 2) Kiaules metai
echo 3) Tigro metai
echo 4) Ziurkes metai
echo.
set /p tkl= Atsakyma irasykite cia:
if %tkl%== 3 goto kkl
if not %tkl%== 3 goto blogai

:kkl
cls
echo Ketvirtasis klausimas:
echo.
echo Geriausia pripazinta interneto narsykle
echo.
echo 1) Mozilla Firefox
echo 2) Internet Explorer
echo 3) Opera
echo 4) Safari
echo.
set /p kkl= Atsakyma irasykite cia:
if %kkl%== 1 goto pkl
if not %kkl%== 1 goto blogai

:pkl
cls
echo Penktasis klausimas
echo.
echo Kas nutape Mona Liza?
echo.
echo 1) Mikelandzelas
echo 2) Kopernikas
echo 3) Tomas Moras
echo 4) Leonardas Da Vincis
echo.
set /p pkl= Atsakyma irasykite cia:
if %pkl%== 4 goto skl
if not %pkl%== 4 goto blogai

:skl
cls
echo Sestasis klausimas
echo.
echo Kokios salies sostine yra Amsterdamas?
echo.
echo 1) Vokietijos
echo 2) Venesuelos
echo 3) Olandijos
echo 4) Prancuzijos
echo.
set /p skl= Atsakyma irasykite cia:
if %skl%== 3 goto septkl
if not %skl%== 3 goto blogai

:septkl
cls
echo Septintasis klausimas
echo.
echo Koks gyvunas yra pats greiciausias pasaulyje?
echo.
echo 1) Gepardas
echo 2) Liutas
echo 3) Suo
echo 4) Arklys
echo.
set /p septkl= Atsakyma irasykite cia:
if %septkl%== 1 goto astukl
if not %septkl%== 1 goto blogai

:astukl
cls
echo Astuntasis klausimas
echo.
echo Kiek saliu priklauso Europos Sajungai?
echo.
echo 1) 24
echo 2) 13
echo 3) 27
echo 4) 12
echo.
set /p astukl= Atsakyma irasykite cia:
if %astukl%== 3 goto devkl
if not %astukl%== 3 goto blogai

:devkl
cls
echo Devintasis klausimas
echo.
echo Firma pavadinimu "Nokia" gamina..?
echo.
echo 1) Kompiuterius
echo 2) Televizorius
echo 3) Mobiliuosius telefonus
echo 4) Buitine technika
echo.
set /p devkl== Atsakyma irasykite cia:
if %devkl%== 3 goto deskl
if not %devkl%==3 goto blogai

:deskl
cls
echo Desimtasis klausimas
echo.
echo Daugiausiai zmoniu gyvena..?
echo.
echo 1) Rusijoje
echo 2) Indijoje
echo 3) JAV
echo 4) Kinijoje
echo.
set /p deskl= Atsakyma irasykite cia:
if %deskl%== 4 goto baigta
if not %deskl%== 4 goto blogai

:baigta
echo.
echo.
cls
echo sveikiname! Jus ka tik atsakete i visus musu sudarytus kalusimus. > sertifikatas.txt
echo.
echo.
cls
goto menu


:blogai
cls
color 4f
echo Pralaimejote
pause
goto menu

:pagalba
cls
echo ==============================================
echo Pagalba
echo ==============================================
echo.
echo.
echo Noredami irasyti atsakyma rasykite: 1, 2, 3 arba 4 ir spauskite "enter"
echo 1 Grizti atgal
echo 2 Iseiti
echo.
set /p pagalb= Pasirinkite:
if %pagalb%== 1 goto menu
if %pagalb%== 2 goto exit

:exit
exit


Report •

#5
February 6, 2010 at 03:58:46
If you have 2 txt files, question and answer, such that line X in each corresponds:

=========================
@echo off & setLocal EnableDELAYedeXpansion

set /a N=0
for /f "tokens=* delims= " %%a in (question) do (
set /a N+=1
set Q!N!=%%a
)

set /a N=0
for /f "tokens=* delims= " %%a in (answer) do (
set /a N+=1
set A!N!=%%a
)

set /a C=0
for /L %%i in (1 1 !N!) do (
set /p R=!Q%%i! ? :
if /i "!R!"=="!A%%i!" set /a C+=1
)

echo you got !C!/!N! correct


=====================================
Helping others achieve escape felicity

M2


Report •

#6
February 6, 2010 at 13:12:35
umm thanks for the code, but where i should put it?

Report •

#7
February 7, 2010 at 04:11:40
Run this modified version, which will create the question and answer files.

Put it in an empty directory.

===============================
@echo off & setLocal EnableDELAYedeXpansion

if not exist question call :makefiles

set /a N=0
for /f "tokens=* delims= " %%a in (question) do (
set /a N+=1
set Q!N!=%%a
)

set /a N=0
for /f "tokens=* delims= " %%a in (answer) do (
set /a N+=1
set A!N!=%%a
)

set /a C=0
for /L %%i in (1 1 !N!) do (
set /p R=!Q%%i! ? :
if /i "!R!"=="!A%%i!" set /a C+=1
)

echo you got !C!/!N! correct

goto :eof

:makefiles

>> question echo hair
>> question echo eyes
>> question echo weight
>> answer echo blonde
>> answer echo blue
>> answer echo 55

goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#8
February 7, 2010 at 06:24:46
thanks a lot, but is it a way to count mistake without making "answer" and "question" files?

Report •

Ask Question