2

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!

5
  • I don't understand your question: Remove C:\users\...File.txt is correct and Remove blablabla is wrong. This looks acceptable from a human point of view. But here we are dealing with a computer: if you create a file, called blablabla, then why would Remove blablabla be wrong? Commented Oct 14, 2018 at 19:34
  • For example let's take another command like "ping" or something, so if the user typed "ping blablabla" it will display an error because "blablabla" is not a valid website name. Like that i want to create a Command line that use custom commands, i tried to search and code but i didn't find the way to do it, By the way thank you for commenting and i hope i will find a solution. - Have a nice day! Commented Oct 14, 2018 at 19:38
  • in a linux shell, you can test if a command has executed successfully or if it has returned an error code. In any case the only test you can rely on without building something really complex, is "did command X complete successfully or not". Again, in linux shell you would write this as if remove ${file} ; then echo "${file} removed OK" ; else echo "ERROR removing $file}"; fi. If cmd.exe is able to execute a similar algorithm, then you have a place to start, but my experience with cmd.exe is 25 years out of date. I do see relatively sophisticated cmd.exe solutions here, so look around. GdLuck Commented Oct 15, 2018 at 12:30
  • Also, your use of the term prompt may be confusing to readers, as the typical meaning is related to the command-line prompt, where echo %prompt% may return something like $P$G, which will display C:\Users\Shellter>. I would rephrase your title as "How to prompt user input in batch file", and edit the rest of the Q to match that idea. Good luck. Commented Oct 15, 2018 at 12:35
  • @shellter Thanks for the suggestions, i will try to find the code to make my dream comes real :D -Have a nice day! Commented Oct 27, 2018 at 13:07

2 Answers 2

4

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'
Sign up to request clarification or add additional context in comments.

3 Comments

Hello, This idea also seems good! it will be better if i can just ask the user for the command once, i mean ask him only one time (Like what CMD does, if you typed "Del file.txt" it will recognize the "Del" command and it will use it on the file). Thanks for your Answer. - Have a nice day!
Yeah, that's a problem with that method. I can't think of any other way to really do it though, as I'm not sure how to pinpoint just a specific section of a response. If you could figure that out though, it could automatically substitute. I'll have to see if I can find anything out about that.
@Sheep1Coder I added in a new one prompt method if you check my edited comment. Took me like an hour lol but I did it!
0

use the doskey command heres what you would put

    doskey del=remove

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.