How to prompt user input in Batch-File - 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!

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'

use the doskey command heres what you would put
doskey del=remove

Related

How to use launch commands in a batch file

Okay, well, I want to make something like OS... But when I start the .bat file there's a set option for going to help and going to the GUI. I have shortcut with -gsys32 - directly to boot the command. Brhfd... Just take a look if the code, I leave some comments.
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface
cmd /k
:help
cls
echo gsys32 - Open the graphical interface.
echo exithelp - exit the help
set /p command=
if %command% ==exithelp goto start
cmd /k
:interface
cls
color 17
#ECHO OFF
echo PROGRAMS - GSYS32
echo DRIVE A:\
dir
#ECHO OFF
echo *Write dir /name of the directory without the slashes/*
cmd /k
:start
cls
#ECHO off
echo GraphicalSystem
echo All rights reserved! 2016
echo PLEASE WRITE help TO OPEN THE HELP WINDOW!
set /p command=
if %command% ==help goto help
if %command% ==gsys32 goto interface // I want this to be executed with the shortcut.
cmd /k
And now the shortcut:
So... I want the command from the screenshot to be executed in the code...
Seems to be just a section of the code, so it's difficult to tell.
set "command=%1"
if not defined command goto noparms
if /i "%command%"=="gsys" goto interface
if /i "%command%"=="help" goto help
echo parameter "%1" not recognised&pause
rem don't know what you want to do now...
...
:noparms
rem there were no parameters supplied
....your posted code
%1 accesses the first parameter supplied to the routine.
you would need -gsys in place of gsys if you want to detect -gsys as a parameter. The match is literal (/i option makes it case-insensitive)
It is windows convention that switch-parameters use the format /gsys - but that's a convention, not a rule.
set /p "var=Promptstring" is the general form for accepting keyboard input. Parameters are read using %1..%9

How to open a program using user input

I am writing a program that will open a program when the user types in: "Open chrome" then it will open chrome. I have this:
SET /P Start=What should i do?
IF /i "%start%"==" open calculator" GOTO CALCULATOR
:CALCULATOR
Start C:\windows\system32\calc.exe
So at Calculator it starts the program calculator.
But i want the user to type in "Open" and the code knows that "Open" = "Start" and whatever comes next is what it has to open.
So the code takes apart what the user types in so if they type in "Open" then it turns that into start and puts it in the code so:
set /p "Input= "
%Input% + %Input2.exe
so the first thing the user types in equals Input and the second thing the user types in equals Input2 so they can type in Start Calc and the code will be: Start calc.exe because it's added a .exe in the second word that the user types in.
When you give me an answer can you please explain what each bit does?
#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
I haven't actually tested this, but it should work.
for /F %%a in (%input%) do (
if %%a == "open" (
if %%b == "chrome" (
REM Do stuff
)
if %%b == "calculator" (
REM Do other stuff
)
)
)

I need help in batch with the "If not defined (item) goto (item)" code

Im creating a parody of the command prompt with batch and everytime i put in an unknown command in the batch file like "JABSD" it just closes even though i already put the
if not defined (item) goto (item)
command. Here is the code:
#echo off
color 0a
echo Microbach Bachdos [version 9.9.bachcmd0a2pro]
echo Copybach (C) 1700 Microbach Corporbachtion. All Bachs reserved.
:commandbach
echo .
set "bachcmd="
set /p "bachcmd= C:\Bachusers\BACH)"
if not defined bachcmd goto invalidbcommand
REM error corrections
if /i "%bachcmd%"==" " (
:invalidbcommand
echo '%bachcmd% is not recognized as an internal or external command, operable program or .bach file.
goto commandbach
)
if /i "%bachcmd%"=="ACTBACH" (
echo Please use a valid number
echo ACTBACH symphony_number
)
REM commands
if /i "%bachcmd%"=="help" (
echo HELP Displays a list of Bach commands.
echo ACTBACH symphony_number Plays the symphony number.
goto commandbach
)
if /i "%bachcmd%"=="ACTBACH 1" (
start https://www.youtube.com/watch?v=x7JZB-lC_Hw
goto commandbach
)
if /i "%bachcmd%"=="ACTBACH 2" (
REM start (insert URL)
echo Cannot find the symphony number
goto commandbach
)
pause
IF NOT DEFINED only checks for the definition of a variable. In your case it will always be true because someone it entering a value.
To accomplish what you want, you need to place this line at the bottom of your script:
GOTO commandbach
This will essentially create an endless loop (until the user closes the window or presses Ctrl+C). You may want to consider adding an "exit" command so users can enter a command to quit your program.

How to find keywords of the users input in the command line?

What I'm looking for is a batch file line of code that will scan what the user inputs to find key words and direct them in the right place. That way when a trainee has a question he/she could just ask the batch file and it will direct them to the proper menu. Is this possible? if so, How would one go about doing this?
:menu
set /p c=Please type you question:
findstr /m "How to ringout a product on our system?" "%c%"
if %c%=="ringout" (
goto :Ringingout
) Else (
goto :Sorry
)
:Ringingout
cls
echo In order to right something out maksure you do the following:
echo - Log in
echo - click on scan in the bottom left had corner on the tender page
echo - scan items
echo - click continue
Pause
goto :Menu
:Sorry
cls
echo Sorry I don't recognize your question, please re-word it.
pause
goto :Menu
#ECHO OFF
SETLOCAL
SET "keywords=word anotherword someword"
FOR %%k IN (%keywords%) DO set "#%%k="
SET /p query="Please enter your query ? "
FOR %%k IN (%keywords%) DO CALL :analyse %%k
SET #
GOTO :EOF
:analyse
CALL SET "found=%%query:%1=%%"
IF "%found%"=="%query%" GOTO :EOF
SET #%1=FOUND!
GOTO :eof
Here's a general way to do it.
If one of the keywords is entered, the variable #keyword will be set to FOUND! so you can use if defined #keyword to process from there.
It's not protected against destructive user-inputs - that's not what this question is about...
You'd be better off collecting all the questions in a document (like .html) and letting the user search that document for what they need. But if this is just an exercise, you can re-write your logic like so to make your program work:
:menu
set "c="
set /p "c=Please type your question: "
echo %c% | findstr /i /b "ringout" >nul
if errorlevel 1 goto Sorry else goto Ringingout
:Ringingout
and so on.

Problems with BAT file: IF/ELSE not working as expected

I'm trying to make a short BAT file and I'm having trouble with one of its functions. I've tried a number of different ways to do this and none of them seem to work, but being a beginner at this I can't figure out the problem. Basically, the script, as it runs, is supposed to check if a certain .BAT file exists, and if it does, the script asks if the user wants to run it. If the user indicates Y, the other BAT is called and then the original script proceeds. If the user indicates N, the script is supposed to proceed without calling the other BAT. So far the script always notices and asks about the file, but choosing Y at the prompt never works. I'm sure the solution is obvious, but it's escaping me. Here's the code:
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
The problem is your variables are being evaluated before they enter the if's, which means cmd won't see any changes until they have ended.
This is causing problems for you as your variables kmpreproc and, depending on the first if result, kmname change within the if blocks.
The fix (presuming the rest of your code is working) is to enable delayed expansion and use delayed expansion instead of normal expansion, by changing the %'s to ! on your variables.
setlocal enabledelayedexpansion
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\!kmname!\!kmname!.flt (
ECHO !kmname!.flt found, will now create !kmname!.ive.
CD C:\Visual\area\!kmname!\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "!kmpreproc!" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\!kmname!\!kmname!.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
Here is below my corrected and tided up code :
#echo off
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%"=="Y" (
GOTO PREPROC
)
GOTO CONTINUE
)
)
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
:PREPROC
ECHO Running Preprocess.bat.
call "cmd /c start Preprocess.bat"
pause
:CONTINUE
ECHO Continuing process.
PAUSE
If it doesn't work try writting setlocal EnableDelayedExpansion under #echo off and replacing all % with ! .

Resources