How to open a program using user input - batch-file

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

Related

Batch Script not Executing filaname + Parameters

My batch script asks the user if he wants to run the program LegoScript.exe
If the user hits 'N' or 'n' then it terminates the program, however if the user types 'Y' or 'y' then it should echo the string "legoscript.exe" and then place the cursor right after it on the same line, so that the user can type additional arguments, and then run the program with the provided arguments, however rather than running the program with the provided arguments, it just shows me a blinking caret indefinitely
Here is the batch script:
#echo off
setlocal enabledelayedexpansion
set "base_path=%userprofile%\\desktop\\LegoScript\\"
for /d %%d in ("%base_path%\V*") do (
set "latest=%%d"
)
set "LATEST=!latest!"
echo Latest folder is: %LATEST%
"%base_path%\\MINGW\\Bin\\gcc.exe" "%latest%\\LegoScript.c" -o "%latest%\\LegoScript.exe"
set /p run=Do you want to run LegoScript.exe? (y/n):
if /i "%run%"=="y" (
#cmd /k ^"cmd /c "echo|set /p="LegoScript.exe "&set /p "arg1= "&& set /p "arg2= "&& set /p "arg3= "&& set /p "arg4= "&& start "" "%latest%\\LegoScript.exe" %arg1% %arg2% %arg3% %arg4%
) else if /i "%run%"=="n" (
exit
) else (
echo Invalid input. Please enter y or n.
pause >nul
call %0
)
Anyone care to figure out how to fix this?
I tried adding extra double quotes and escape characters ('^') but no luck.

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.

(Windows Batch File) IF statement inside FOR LOOP

I'm trying to write manual document validation part of my program. It's basically opening all the pdf documents one by one in the same folder. When its open i would like to echo few possibilities for user. Here starts the problem. I have around 180 possible choices. I was thinking to ask for the first letter of choice. Then it will echo all choices with started with X letter and user has to simply enter the number of this choice. So for example we have :
1. Asomething
2. Asomename
3. Asomenametoo
4. Bname
5. Bname 2
6. Bname 3
I want user to choose first letter and print possible choices. When the choice is made program should add some string to txt file with the same name in the same folder. Here i have a problem with IF statement inside FOR loop. I wanted to use goto but i can't do it inside FOR loop.
I can set up all the strings for each number before. For example : When you choose 1 it will add SomeString to txt. It's important to use choice option to avoid any typo's. Does anybody knows any other way to do this inside FOR loop ?
CODE:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
I'm having some trouble understanding the problem you're having, but it looks like all of the statements following the IF should all be conditions of the IF, not just the ECHO statement. For that, you can put the entire block in parentheses like this:
setlocal enabledelayedexpansion
FOR %%b IN (c:\test\*.txt) DO (
IF "%ERRORLEVEL%"=="0" (
ECHO Document will open now...
start Acrobat.exe %%b.pdf
ECHO 1. Sample 1
ECHO 2. Sample 2
set /p choice= Please enter number:
call :OPTION
ECHO !choice! >> %%b
PAUSE
taskkill /IM Acrobat.exe >> c:\test\log\temp.txt
) else (
goto :EOF REM Just an example of else
)
)
PAUSE
GOTO MENU
:OPTION
IF !choice!==1 SET /A !choice!==MNV666
IF !choice!==2 SET /A !choice!==MNV777
GOTO:EOF
Were you having some problem using goto in the FOR loop?

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