将句子存储在变量中(批处理)

时间:2015-09-19 18:04:30

标签: windows batch-file cmd

我正在创建批处理文件,并且只要它们是用户输入字段,用户输入单词或句子并将其存储在文本文件中。我完成了所有的工作。

但是当用户的输入超过1个字时,会出现以下错误:

*second word* was unexpected.

如何解决这个问题?

@echo off
color f0
:menu
cls
echo Batch Editor
echo Choose what you want to do:
echo 1) New Document
echo 2) Load existing document
echo 3) Learn How to Use
echo 4) Exit
set /p menu=
if %menu%==1 goto new
if %menu%==2 goto load
if %menu%==3 goto learn
if %menu%==4 goto exit
if [%1]==[]  goto learn
echo Invaild Code
pause
goto menu 

:new
cls
echo You have chosen to start a new document.
echo But please note that is impossible to know how to code
echo without seeing the "How to Learn" section.
echo Choose:
echo 1) Continue anyways
echo 2) Go to the "How to Learn" section
echo 3) Exit
set /p type=
if '%type%'=='' goto new
if %type%==1 goto newdoc
if %type%==2 goto learn
if %type%==3 goto exit
echo Invaild Key
pause
goto new

:newdoc
cls
set /p input=
if %input%==Qsave goto saving
if %input%==qsave goto saving
if %input%==qSave goto saving
if %input%==QSAVE goto saving
goto newdoc1
pause

:newdoc1
set /p chosen=
if '%chosen%'==''(
goto newdoc2)
else if "%chosen%"=="Qsave" (
goto saving)
else if "%chosen%"=="qsave" (
goto saving)
else if "%chosen%"=="qSave" (
goto saving)
else if "%chosen%"=="QSAVE" (
goto saving)
else (
goto newdoc2)


:newdoc2
set /p chosen=
if '%chosen%'==''(
goto newdoc1)
else if %chosen%==Qsave (
goto saving)
else if %chosen%==qsave (
goto saving)
else if %chosen%==qSave (
goto saving)
else if %chosen%==QSAVE (
goto saving)
else (
goto newdoc1)
:saving
cls
echo Saving
pause

:load
echo LOADING
pause

:learn
echo Learn
pause

:exit
cls
set /a exiting=0
echo Are you sure you want to exit?
echo Choose:
echo 1)Exit
echo 2)Go Back to the Main Menu
set /p exiting=
if %exiting%==1 exit
if %exiting%==2 goto menu
echo Invaild key
pause
goto exit
pause
exit

1 个答案:

答案 0 :(得分:0)

set var=Alpha beta Gamma
if %var%==whatever

被翻译为:

if Alpha beta Gamma==whatever

IF语法为IF <Argument1> <comparator> <Argument2> <command>

Argument1为Alpha,比较者为beta - 等等,这是错误的......

导致“beta意外”,因为应该有一个compatator(==),而不是一个Argument。

更好的语法:

if "%var%"=="whatever"

被翻译为:

if "Alpha beta Gamma"=="whatever"

Argument1为"Alpha beta Gamma",Comparator为==,Agrument2为"whatever" - 精细语法。