Solved How to save user input from a batch runtime to a file.

November 17, 2014 at 17:55:24
Specs: Windows 8.1
I'm trying to make a quiz type thing for a school project. I know how to prompt for Y/N and variable input however the only way I know how to output data requires that you rewrite it each time something is added to it. I want the file to save answers, compare the user created answer to a preset answer then read back that file in the original window. A way to calculate the amount of matching and mismatched variables would be helpful too.

message edited by Meldrex5

See More: How to save user input from a batch runtime to a file.

Report •

November 17, 2014 at 22:12:24
✔ Best Answer
The simplest method is just to append to the file, but here is a refinement that saves creating an ever-increasing file, (but re-writes it from scratch each time):
@echo off & setlocal
set query1=whats your favorit color
set query2=whats your name
set query3=whats the distal relationship of square root of pi to radial offset in minutes per degree per second
for /f "tokens=*" %%a in (user.txt) do set %%a
echo last results:
set response 2>nul||echo nothing set
:: setting for three queries
for /L %%z in (1 1 3) do (
echo last response to !query%%z!?: !response%%z!
:: optional, but recommended: clear varibles for each new run
echo will now be cleared
set response%%z=
:: get new response
set /p response%%z=!query%%z!?
::... add any number of queries in this block till done.

:: now, when done:
::>>user.txt echo response1=%response1%
::>>user.txt echo response2=%response2%
:: or, to save time, just: set response >> user.txt
set response > user.txt

:: view file
for /f "tokens=*" %%a in (user.txt) do set %%a
set response 2>nul||echo nothing set

::------- end batch ------- the rest (or creative alternatives) up to you.

message edited by nbrane

Report •

November 18, 2014 at 18:20:13
I was going to do something like this:
@echo off
echo Intro text here
echo Question 1: (querey here)
set /p answer1=
::this question-input format repeated however many times as nesescary
echo Quiz end
if ANSWER== %answer1% echo question 1 correct
if not ANSWER== %answer1% echo wrong, the correct answer was ANSWER
::grading portion repeated as well

I'm not sure what the "for" command is how how its used so you'll have to explain that. Also keep in mind that I have a very limited knowledge of what I'm doing.

Report •

November 19, 2014 at 17:03:59
The "for" command is the massively labor-saving device of looping as opposed to linear logic. Also the only way to read a text file into local batch variables. To see how "FOR" is or can be used:
FOR /?
Mainly, in my code, it's used to read a file (user.txt) and to prompt and collect answers, (stored in a serial variable 'response') which is then written out to a results file (same as input file: 'user.txt'). If you run my batch, and experiment a bit, you will readily see how it works. I promise it's not malevolent, or else the forum moderators would shut me down and permanently evict me in a heartbeat. Storage of data is essential to any serious attempt to evaluate and study results of testing.
ps: forgot this in line one:
@echo off & setlocal
should be:
@echo off & setlocal enabledelayedexpansion

message edited by nbrane

Report •
Related Solutions

Ask Question