如何使用用户输入打开程序

时间:2015-06-14 15:17:15

标签: batch-file input

我正在编写一个程序,当用户键入时打开一个程序:“打开chrome”然后它将打开chrome。我有这个:

SET /P Start=What should i do? IF /i "%start%"==" open calculator" GOTO CALCULATOR

:CALCULATOR Start C:\windows\system32\calc.exe

所以在计算器上启动程序计算器。 但我希望用户输入“打开”,代码知道“打开”=“开始”,接下来的是它必须打开的内容。

因此代码会分解用户键入的内容,因此如果他们输入“Open”,那么它会将其转换为start并将其放入代码中: set /p "Input= " %Input% + %Input2.exe 所以用户输入的第一件事就是输入,用户输入的第二件事就是输入2,所以他们可以输入Start Calc,代码将是:启动calc.exe,因为它在用户的第二个单词中添加了一个.exe类型。

当你给我一个答案时,你能解释一下每个位的作用吗?

2 个答案:

答案 0 :(得分:0)

我实际上没有对此进行测试,但它应该有效。

for /F %%a in (%input%) do (
 if %%a == "open" (
  if %%b == "chrome" (
   REM Do stuff
  )
  if %%b == "calculator" (
   REM Do other stuff
  )
 )
)

答案 1 :(得分:0)

@echo off
setlocal

:nextCommand
echo/
set /P "input=What should I do? "

if /I "%input%" equ "exit" (
   echo Bye...
   exit /B
)

rem Try to change "open" by "start" in the input line
set "changed=%input:open=start%"

rem Check if the input changed
if "%changed%" equ "%input%" (
   echo I don't know how to %input%
   goto nextCommand
)

rem Execute the changed line adding ".exe" at end
%changed%.exe
echo Done!
goto nextCommand
相关问题