User input not recording properly.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 20 Dec 2018 09:45

User input not recording properly.

#1 Post by gwalnum » 20 Dec 2018 09:54

Wrote batch file that offers a menu of choices in which the user can select multiple answers by entering numbers that corresponds with the answer separated by a space. I have to open a CMD first then run the script from there. When I do the first time I run it, the answers are not created. Run the script a second time from the same command window and it records the answers made the first time. Run a third and it records the answers from the second time and so on. If I just run the script without opening a command window it doesn't record anything. It should write users answers to a text file.

Code: Select all

@Echo off
SET/P  MonDef=Does Monitor have any defects? (y or n): %=%
IF /i %MonDef%==n (
Set text=Does Monitor have any defects? There are no cracks or other physical defects in monitor. 
Echo %text%
CALL %USERPROFILE%\Desktop\WriteText.bat
) Else If /i %MonDef%==y (
Echo The display is cracked ---- 1
Echo The display is dim -------- 2
Echo The display is blurry ----- 3
Echo The display is discolored - 4
SET /p DisplayDef=Please type in the numbers of that corresponds with the display issues separated by a space: %=%
FOR %%i IN (%DisplayDef%) DO (

	IF %%i==1 (
		Echo Test 1
		SET text=Monitor is cracked.
		CALL %USERPROFILE%\Desktop\WriteText.bat
		) ELSE IF %%i==2 (
		Echo Test 2
		SET text=Monitor is dim.
		CALL %USERPROFILE%\Desktop\WriteText.bat
		) ELSE IF %%i==3 (
		Echo Test 3
		SET text=Monitor is blurry
		CALL %USERPROFILE%\Desktop\WriteText.bat
		) ELSE IF %%i==4 (
		Echo Test 4
		SET text=Monitor is discolored.
		CALL %USERPROFILE%\Desktop\WriteText.bat
Last edited by aGerman on 20 Dec 2018 15:39, edited 1 time in total.
Reason: Please use code formatting.

Posts: 3441
Joined: 22 Jan 2010 18:01
Location: Germany

Re: User input not recording properly.

#2 Post by aGerman » 20 Dec 2018 15:48

Variables in a command line or a block of command lines enclosed in parentheses, are expanded to their values only once before the command line or block is executed. To avoid this early expansion you have to use delayed variable expansion by setting

Code: Select all

setlocal EnableDelayedExpansion
For all variables defined or changed in a parenthesized block, replace the surrounding percent signs with exclamation points.


Post Reply