batch-file - 如何在批处理文件中提示用户输入

标签 batch-file input command-line

我正在尝试使用批处理文件制作自己的命令提示符,其中包含自定义命令,例如“删除”作为“Del”命令等...但是当我遇到用户输入时,我遇到了问题,这里是是:

CMD.bat

@echo off
echo.
set /p inp=Command: 
if /i %inp% == Remove ...
...

我停下来思考,“我如何发出删除命令?”。所以我想做的是创建一个“删除”命令来使用它,就像这样“删除 C:\Users\usr\Desktop\File.txt”,但是如果用户输入另一个类似删除 blablabla 的内容,程序将如何检测到它命令语法不正确?.

因此,如果有人找到解决方案,我将非常感激,谢谢!

最佳答案

假设您询问如何设置自定义命令,请尝试此操作。

 @echo off
set "RESPONSE="
goto 'input'

: 'input'
set /p response=What would you like to do?
if /I %response%==help goto 'help'
set /p responsetwo=What would you like to %response%?
if /I %response%==remove set response=del
if /I %response%==check set response=dir
if /I %response%==dir %response% "%responsetwo%"
%response% %responsetwo%
echo %response% "%responsetwo%"
goto 'input'

: 'help'
cls
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
pause
goto 'input'

要添加更多自定义命令,只需添加

if /I %response%==<word you want to do X command> set response=<X command>

(显然,将 X 替换为第二个代码段的命令。)

编辑:好的,在阅读您的评论后,我想出了一个更好的解决方案。给你!

@echo off
goto 'input'

: 'input'
cls
set "response="
set /p response=What would you like to do?
set firstresponse=%response:~0,5%
if %firstresponse%==help goto 'help'
pause
if /I %firstresponse%==check set firstresponse=dir && set executeparttwo=%response:~5%
if /I %firstresponse%==remov goto 'remove'
rem Put "if /I %firstresponse%==<whatever the first 5 letters of the command would be> goto '<command name>'
%firstresponse%%executeparttwo%
pause
goto 'input'

: 'remove'
set "firstresponse=" && set firstresponse=%response:~0,6%
if /I %firstresponse%==remove set firstresponse=del
set executeparttwo=%response:~6%
%firstresponse%%executeparttwo%
pause
goto 'input'

: 'help'
cls
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
pause
goto 'input'

关于batch-file - 如何在批处理文件中提示用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52805669/

相关文章:

java - 使用 Hibernate 进行批量保存的更快方法?

C++ SDL 键盘响应

c - 如何在类 Unix shell 中保存交互式命令行程序的输出?

javascript - 使用 javascript 更改 AngularJS 输入文本值

c - 检测输入中的空白

bash - 是否有shell命令递归地授予目录和文件权限?

windows - 随机化批处理文件中的文本颜色和背景颜色

windows - 创建包含文件名、创建日期、修改日期的列表

windows - 使用批处理文件将文件从本地文件夹复制到 "C:\Windows\System32\"

windows - 使用批处理文件计数文件