문제

I have been working on a dual batch file in which you ask a question, the question gets saved as a text file, then another batch opens the question, finds an answer for it, creates another text file with the answer, which then gets retrieved from the first batch. This works fine up to the point where I want some questions to be considered "core commands". For example, shutdown. So, in the batch file that treats the questions, if a command such as "shutdown" is entered, the answer is "qwertui" (first letters of the keyboard). It also creates a file called "shutdown.txt". So, in the first batch, if qwertyui is detected, it sets the variable "core" as "oui". (Please note that in my actual code, on startup core is set as "non".) However, in the part of the code where it detects if core is oui, it says ==Oui is unexpected at this time and crashes the batch.. Here is the code for the first file (help) where the user asks the question:

@echo off
:MAIN
REM ***
REM This is the main command section.
REM ***
cls
set name=%username%
 echo [Sebb] Welcome, %name%. How may I help you?
 echo.
 echo.
 echo.
set /p command=">> "
 echo.
 echo.
 echo.
 echo Thinking...
GOTO SECTIONCORE
:MAINSECOND
REM User is brought here for the 1< question input.
 set /p command=">> "
 echo.
 echo.
 echo.
 echo Thinking...
GOTO SECTIONCORE
:SECTIONRESULTS
 echo.
  set finalresult=%result%
 echo [Sebb] %result%
 echo.
 echo.
 echo.
REM THIS PAUSE IS TEMPORARY, ONLY FOR TESTING:
pause
if core=oui (
goto SECTIONCOREQUESTION
)
GOTO MAINSECOND
:SECTIONCOREQUESTION
REM If user is here, it is because the entered command was of the core quality (e.g. shutdown)
REM Checks for possible core answers:
if result=shutdown (
 PING 1.1.1.1 -n 1 -w 3000 >NUL
 exit
GOTO MAINSECOND
)
REM If all the answers are false, enter this message:
echo [Sebb] Error: Your question was reported as a core question, but cannot be processed.
echo [Sebb] Ignoring...
 PING 1.1.1.1 -n 1 -w 1000 >NUL
GOTO MAINSECOND



:SECTIONCORE
echo %command%>> comm.txt
PING 1.1.1.1 -n 1 -w 3000 >NUL
:SECTIONCORE2
REM Checks if text has been written in comm2.txt
  IF EXIST comm2.txt (
  GOTO SECTIONCORE3
  )
 PING 1.1.1.1 -n 1 -w 3000 >NUL
 GOTO SECTIONCORE2
:SECTIONCORE3
set /p result=<comm2.txt
del /s comm2.txt>nul
REM Checks if the command was a core command (e.g. shutdown) because the result would be qwertyui.
if "%result%"=="qwertyui" (
   set core=oui
   )
GOTO SECTIONRESULTS

And here is the code for the batch in which the question gets treated:

@echo off
 echo Waiting for prompt..
:SECTIONCHECK
REM Checks if text has been written in comm.txt
  IF EXIST comm.txt (
  GOTO MAIN
  )
 PING 1.1.1.1 -n 1 -w 3000 >NUL
 GOTO SECTIONCHECK

:MAIN
set /p command=<comm.txt
del /s comm.txt>nul
echo User input command "%command%"
GOTO CORE
:MAIN2
echo Answer="%answer%"
echo %answer%>> comm2.txt
GOTO SECTIONCHECK
:CORE
echo.
REM CODE BEGINS
echo Processor:
REM First, looks for primary commands such as shutdown..
echo Checking core commands
echo.
echo Shutdown=
if /i %command%==shutdown (
 set answer=Shutting down the system...
  echo %answer%>> shutdown.txt
  echo True
 GOTO MAIN2
)
echo False
echo.
GOTO MAIN2

Note: Both of the codes are only sections of my actual program, but the same problem happens in both versions. There should be nothing not included in here causing the problem.

Please test out the code for yourself by saving both of the codes as two different batch files, and opening them both. Then, write anything as a "command" and it will bring you to a temporary "pause". After pressing any key to continue, you will see "oui was unexpected at this time" very quickly before the file then crashes. Any help would be greatly appreciated! :) -Sebb EDIT: Also, if the command/question you ask has spaces in it (i.e. is more than one word long) it says thefirstwordofyourcommand was unexpected at this time in the second batch file...

도움이 되었습니까?

해결책

at a glance:

if core=oui (
goto SECTIONCOREQUESTION
)

should be :

if defined core if "%core%" EQU "oui" (
goto SECTIONCOREQUESTION
)

다른 팁

Ok. I modified your code in order to made it shorter and more readable. The code below is equivalent to the yours, but written in a different way. This is the first part:

@echo off

cls
echo [Sebb] Welcome, %username%. How may I help you?
:MAINSECOND
REM User is brought here for the 1< question input.
set /p command=">> "
echo Thinking...

:SECTIONCORE
echo %command%>> comm.txt
PING 1.1.1.1 -n 1 -w 3000 >NUL
:SECTIONCORE2
REM Checks if text has been written in comm2.txt
IF NOT EXIST comm2.txt (
   PING 1.1.1.1 -n 1 -w 3000 >NUL
   GOTO SECTIONCORE2
)
:SECTIONCORE3
set /p result=<comm2.txt
del comm2.txt
REM Checks if the command was a core command (e.g. shutdown) because the result would be qwertyui.
if "%result%" equ "qwertyui" set core=oui

:SECTIONRESULTS
echo/
echo [Sebb] %result%
REM THIS PAUSE IS TEMPORARY, ONLY FOR TESTING:
pause
if "%core%" neq "oui" goto MAINSECOND

REM If user is here, it is because the entered command was of the core quality (e.g. shutdown)
REM Checks for possible core answers:
if "%result%" equ "shutdown" (
   PING 1.1.1.1 -n 1 -w 3000 >NUL
   exit
)

REM If all the answers are false, enter this message:
echo [Sebb] Error: Your question was reported as a core question, but cannot be processed.
echo [Sebb] Ignoring...
PING 1.1.1.1 -n 1 -w 1000 >NUL
GOTO MAINSECOND

And this is the second part:

@echo off

echo Waiting for prompt..
:SECTIONCHECK
REM Checks if text has been written in comm.txt
IF NOT EXIST comm.txt (
   PING 1.1.1.1 -n 1 -w 3000 >NUL
   GOTO SECTIONCHECK
)

:MAIN
set /p command=<comm.txt
del comm.txt
echo User input command "%command%"

:CORE
echo/
REM CODE BEGINS
echo Processor:
REM First, looks for primary commands such as shutdown..
echo Checking core commands
echo/
echo Shutdown=
if /i "%command%" equ "shutdown" (
   set answer=Shutting down the system...
   echo %answer%>> shutdown.txt
   echo True
) else (
   echo False
)

:MAIN2
echo Answer="%answer%"
echo %answer%>> comm2.txt
GOTO SECTIONCHECK

Reviewing these codes an important detail immediately appears. If these are two parts of the same program, then the next section:

:SECTIONCORE2
REM Checks if text has been written in comm2.txt
IF NOT EXIST comm2.txt (
   PING 1.1.1.1 -n 1 -w 3000 >NUL
   GOTO SECTIONCORE2
)

... will never exit, because there is no way that another process write to comm2.txt. In order for this method to work, you need to separate your code in two different programs and start both of them in a simultaneous way (that may lead to further synchronization problems). Anyway, I don't understand why the processing can not be done in the same program...

I encourage you to review the design of your program and solve the problems before write the code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top