How to have user input string with spaces?

February 14, 2011 at 09:26:43
Specs: XP
I am trying to create a log generator that will allow multiple lines of input from the user and add it to a text file.

Current process (temp log is created constantly during script):
1. Asks for user's initials.
2. Asks for problem/issue (allows for multiple lines of input, until there is a blank line.
3. Does the same as step 2 for resolution.
4. Finishes log and copies from temp to permanent log file.

My problem is that my script limits each line of user input to one word (no spaces allowed).

How can I allow a variable/string to be input without the user manually adding quotes ("") before and after each line?


@ECHO off
SETLOCAL ENABLEEXTENSIONS
SET ver=1.0.0
COLOR 4f
TITLE Automatic Log Creator (v.%ver%)...
SET cp=xcopy /y /h
SET dl=del /q
SET date=%date:~4,2%.%date:~7,2%.%date:~-4%-%time:~-11,2%.%time:~-8,2%
SET if=IF EXIST
SET log=C:\updates\Temp.log
SET log2=C:\updates\Updates.log
SET spt=___
SET spe=...
SET spl=---------------------------------------------
:: BEGIN SCRIPT
:title
cls
ECHO ----------------------------------
ECHO Welcome to the OEMDD Log Creator
ECHO ----------------------------------
ECHO.
:start
SET /P USER=Please enter your initials (i.e. NCB)?
ECHO.
ECHO %spl%>>%log%
ECHO # Activity Log on %date% #>>%log%
ECHO # Changes made by %USER% #>>%log%
ECHO %spl%>>%log%
ECHO. >>%log%
::ISSUE LOG
:issue
ECHO Issue(s) Addressed:>>%log%
ECHO Describe the issue you are addressing (multi-line):
SET ISSUE=nul
SET /P ISSUE=
if /i {%issue%}=={nul} (goto :issue-error)
ECHO *%issue%>>%log%
if /i {%issue%}=={%issue%} (goto :issue-next)
echo error
pause
:issue-next
SET ISSUE=nul
SET /P ISSUE=
if /i {%issue%}=={nul} (goto :resolution)
ECHO *%issue%>>%log%
if /i {%issue%}=={%issue%} (goto :issue-next)
:issue-error
ECHO You must enter at least 1 line:
goto :issue-next

::RESOLUTION LOG
:resolution
ECHO Action/Resolution Completed:>>%log%
ECHO Describe the actions/resolution taken (multi-line):
SET RESOLUTION=nul
SET /P RESOLUTION=
if /i {%resolution%}=={nul} (goto :resolution-error)
ECHO *%resolution%>>%log%
if /i {%resolution%}=={%resolution%} (goto :resolution-next)
echo error
pause
:resolution-next
SET RESOLUTION=nul
SET /P RESOLUTION=
if /i {%resolution%}=={nul} (goto :end)
ECHO *%resolution%>>%log%
if /i {%resolution%}=={%resolution%} (goto :resolution-next)
:resolution-error
ECHO You must enter at least 1 line:
goto :resolution-next

::FINISH
:end
ECHO. >>%log%
ECHO %spl%>>%log%
ECHO # End changes on %date% #>>%log%
ECHO %spl%>>%log%
ECHO Thank you, %USER%%spe%
ECHO.
ECHO Log completed successfully.
TYPE %log%>>%log2%
cls
ECHO LOG RESULTS:
ECHO.
type %log%
%dl% %log%
PING localhost -n 10 >nul
goto :title


See More: How to have user input string with spaces?

Report •

#1
February 14, 2011 at 10:15:32
When comparings strings embrace with double quotes NOT other chars so you can process string with spaces, i.e.

if /i "%issue%"=="nul" (goto :resolution)
ECHO *%issue%>>%log%
if /i "%issue%"=="%issue%" (goto :issue-next)


Report •

#2
February 14, 2011 at 10:28:57
Thanks for the reply IVO, by replacing the curly braces with quotes I can no longer proceed to the next step when the user's input is blank.

My problem is with the second line below. I want users to be able to enter a sentence, or at least a few words for ISSUE w/o needing to use quotes. (single words/characters work fine)

Is it possible to use SET to enter more than one word without quotes?

SET ISSUE=nul
SET /P ISSUE=
if /i {%issue%}=={nul} (goto :issue-error)
ECHO *%issue%>>%log%
if /i {%issue%}=={%issue%} (goto :issue-next)


Report •

#3
February 14, 2011 at 10:49:08
You can enter whatever you want without embracing with double quotes (even a sequence of spaces); the issue variable is completed by pressing the Enter key, i.e.

This string will be stored into ISSUE

-           -

The above too (- just to mark the beginning and the end).

So i can't understand your post and I repeat you MUST use double quotes inside an IF statement to compare strings with blanks.


Report •

Related Solutions

#4
February 14, 2011 at 11:13:34
Oh, I see what you mean..
if I do this manually in cmd:

set /p issue=
test
echo %issue%
returns: test
if /i %issue%==%issue% (echo match)
returns: match

it works.

But if I do this:

set /p issue=
test 2
echo %issue%
returns: test 2
if /i %issue%==%issue% (echo match)
returns: 2==test was unexpected at this time.

Is there another way to do this?


Report •

#5
February 14, 2011 at 11:17:06
Just got it!

if I change this line..

if /i %issue%==%issue% (echo match)

to this..

if /i "%issue%"=="%issue%" (echo match)

then it will work.
Thank you so much for helping!!!


Report •

Ask Question