1

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?

Atlas 7
  • 27
  • 1
  • 4
  • I think your issue is similair to this ==> http://stackoverflow.com/questions/30823681/how-to-check-and-correct-user-input-when-he-omit-the-extension-exe-to-kill-the/30828097#30828097 – Hackoo Jun 15 '15 at 00:10

2 Answers2

0

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
  )
 )
)
Script_Coded
  • 709
  • 8
  • 21
  • Thanks for the reply but it just closes down so i added a pause at the end to see if it mentioned the error but it doesn't. Thanks anyway! – Atlas 7 Jun 14 '15 at 15:31
0
@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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks it works well ! but some programs don't open even though i type them in properly :/ Thanks for the help though. – Atlas 7 Jun 15 '15 at 08:15
  • My code should open any `program.exe` file when you enter `open program`, in the same way as enter `start program.exe` in the command line (that was what you requested). Could you post an example when this point fails? – Aacini Jun 15 '15 at 16:28
  • I type in "Open Steam" and it doesn't. The Caps are all right and even though it's in 'Program Files (x86)' it's not opening... :/ – Atlas 7 Jun 15 '15 at 17:01
  • ... and when you type "Start Steam.exe" it works correctly? If not, then the problem is _not_ in my code... – Aacini Jun 15 '15 at 17:45
  • Excuse me? Two points here: **1.** When you _run my code.bat_ and type "Open Steam" the program not work. Ok at this point. **2.** I asked you to type "Start Steam.exe" _from the command line_ **instead** run my code.bat. Why? Because my code.bat just translate `Open Steam` input into _the execution_ of `Start Steam.exe` command (as you requested). So, in order for "Open Steam" works in my code.bat program, it is a requisite that "Start Steam.exe" works from the command line! "If not, then the problem is _not_ in my code"... – Aacini Jun 15 '15 at 19:06
  • I made a new batch file to just "Start Steam.exe" and it didn't work so it's not your code it's my computer. I didn't mean to offend you when i said that your code didn't work, thanks for the help. – Atlas 7 Jun 17 '15 at 12:51