Sheep1Coder
Sheep1Coder

Reputation: 55

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:

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?.

So if anyone Found a solution i will be very appreciated, And Thanks!

Upvotes: 2

Views: 27117

Answers (2)

user11376279
user11376279

Reputation:

use the doskey command heres what you would put

    doskey del=remove

Upvotes: 0

Kerrmunism
Kerrmunism

Reputation: 56

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.)

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'

Upvotes: 4

Related Questions