简体   繁体   English

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

[英]How to prompt user input in Batch-File

I'm trying to make my own Command Prompt with a Batch-File with a custom commands like "Remove" as "Del" command etc... But when i came across the User input i faced a problem and here it is: 我正在尝试使用带有自定义命令(例如“删除”作为“ Del”命令等)的批处理文件来制作自己的命令提示符……但是,当我遇到用户输入时,我遇到了一个问题,这是:

CMD.bat 蝙蝠

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

And i stopped to think, "How do i will make a Remove command?". 我停下来想,“我将如何执行删除命令?”。 So what i want to do is making a "Remove" command to use it like this "Remove C:\\Users\\usr\\Desktop\\File.txt" but if the user typed another thing like Remove blablabla, how the program will detect that the command syntax is incorrect?. 因此,我要执行的操作是使用“删除”命令来像“删除C:\\ Users \\ usr \\ Desktop \\ File.txt”这样使用它,但是如果用户键入了诸如“删除blablabla”之类的其他内容,程序将如何检测到该命令语法不正确?

So if anyone Found a solution i will be very appreciated, And Thanks! 因此,如果有人找到了解决方案,我将非常感激,谢谢!

Assuming that you are asking how to set custom commands, try this. 假设您询问如何设置自定义命令,请尝试此操作。

 @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'

To add anymore custom commands, simply add 要添加更多自定义命令,只需添加

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

(Replace X with command for second code piece, obviously.) (显然,用第二个代码段的命令替换X。)

EDIT: Okay, so after reading your comment I came up with a better solution. 编辑:好的,所以在阅读您的评论后,我想出了一个更好的解决方案。 Here you go! 干得好!

@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'

使用doskey命令在这里输入

    doskey del=remove

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM