2

I have written a .bat file in windows. Which launch these url in browser

do some other task
START www.google.com
START www.microsoft.com
START www.apple.com

But the one I wrote is static which execute all together.

I would like to make it accept input and execute that command based on input.

For example

input 1 launch only www.google.com

input 2 launch only www.microsoft.com

input 3 launch only www.apple.com

input 4 do some other task or run a command

Also looking to open launch .bat file with associated keyboard shortcut.

if it matter I am gonna use this on Windows 10

Any help would be appreciated.

Thanks

4
  • I suggest to learn AutoHotkey for user-input automatization and macros. You need to invest about one hour to understand how it works, but then... infinite possibilities!
    – Lluser
    Dec 16, 2020 at 9:15
  • @Lluser - OP asked for .bat, @ OP - Google for how to write bat scripts, how to input variables, how to execute parse input,... SU is to help you when you get stuck ("I'm gonna use it on Windows 10" says you didn't try?), so show us what you investigated, tried and where you got stuck and if you could explain the tag "keyboard-shortcuts" as you do not mention them in your post
    – Zina
    Dec 16, 2020 at 9:41
  • @zina I have added sample .bat which is working fine atm. But am looking to advance it make it accept the input and execute command accordingly.
    – ankitd
    Dec 16, 2020 at 10:16
  • there are a lot of resources on the Internet with how to script...please check the superuser tour - your script works fine but shows no attempt for a solution, SU is not intended to be a "can you write me a script" - it is for helping you to get it done. please research and provide your tries
    – Zina
    Dec 16, 2020 at 10:42

3 Answers 3

0

Could be something like this:

@echo off
echo.
echo Please specify the first site you would like to open:
set /p "Input1=Site: "
START %Input1%

echo Please specify the second site you would like to open:
set /p "Input2=Site: "
START %Input2%

echo Please specify the third site you would like to open:
set /p "Input3=Site: "
START %Input3%
echo.
echo Please specify a command you would like to run:
set /p "Input4=Command: "
%Input4%
pause
1
  • 1
    this is not what the OP wanted, he wants the script to ask what you want to do and depending on the answer he wants different actions to be performed.....
    – Zina
    Dec 16, 2020 at 10:43
0

So after some search I was able to do this.

basically to accept input on .bat file you need this

set /P input="What you gonna do? "

and then one can use this as a variable

@echo Hello %input%!

To compare the input value, we can use IF statement and make it execute desired command

IF %input% == 1 cmd /k "yarn"
IF %input% == 2 cmd /k "yarn start"
IF %input% == 3 cmd /k "yarn dev"
IF %input% == 4 START www.google.com
IF %input% == 5 START www.apple.com
IF %input% == 6 cmd /k "cd changefolder"
IF %input% == 7 call another.bat

Final thing launching with keyboard-shortcut One can not assign keyboard-shortcut to .bat file directly. But you can assign to Shortcut. To do this

  1. Right Click on .bat file and Click on Create Shortcut

enter image description here

  1. Right Click on newly created Shortcut. Go to Properties. Enter desired key as shortcut. Click Ok

enter image description here

Booom. you are good to go.

So mine whole .bat file looks like this

@echo off
cls && cd pathtosomefolder

set /P input="Which command, My lord? "
@echo Running %input%!

IF %input% == 1 cmd /k "yarn"
IF %input% == 2 cmd /k "yarn start"
IF %input% == 3 cmd /k "yarn dev"
IF %input% == 4 START www.google.com
IF %input% == 5 START www.apple.com
IF %input% == 6 cmd /k "cd changefolder"
IF %input% == 7 call another.bat

2
  • nice, even with your initial request different than your solution ;) but this is what I wanted to achieve, to give you a little push to the right direction...and you seem to have created what you wanted/need
    – Zina
    Dec 16, 2020 at 11:59
  • for this special case, you should consider using the choice command (for one digit/letter input). It accepts only valid inputs.
    – Stephan
    Dec 25, 2020 at 18:05
0

// Edit

Some option to check input and run your command:

@echo off & setlocal

cd /d "D:\PathToSomeFolder"
title <nul & title ..\%~nx0

%:^)
set "_input=" & cls & echo/
echo/ Here are your command options: 
echo/ & type "%~f0"|findstr /b .Option 
for /f %%i in ('echo\prompt;$h^|cmd')do set "_bs=%%~i"

set /p "_opts= %_bs% What you gonna do? " || goto %:^)
echo=%_opts%|2>nul findstr /be [1-7] >nul || goto %:^)

for /f ^usebackq^tokens^=2^delims^=^= %%i in (`^<con: ^<nul ^
type "%~dpnx0"^|findstr /b .Option.%_opts%`)do <con: call %%~i

endlocal && goto :eof

 Option 1 = cmd /c "yarn"
 Option 2 = cmd /c "yarn dev"
 Option 3 = cmd /c "yarn start"
 Option 4 = cd /d "changefolder"
 Option 5 = start "" "www.apple.com"
 Option 6 = start "" "www.google.com"
 Option 7 = cmd /c "call another.bat"

Obs.: Use spaceOption 1 = command

Edit //


You can use one loop from 1 to 5 to define your input with set /p and use the if condition to check if you were going to use explorer to open the folder, start your browser or run your command...

@echo off 

setlocal EnableDelayedExpansion

:reset_cmd
set "_arg=" & cls 
for /l %%L in (1,1,5)do set "_input="

for /l %%L in (1,1,5)do set "_arg=%%~L" && <con: ^
     set /p "_input%%~L=Enter input %%~L: " || <nul ^
     goto :reset_cmd

for /l %%L in (1,1,!_arg!)do (
     if /i "!_input%%~L:~0,4!" == "yarn" (
         <con: rem./ &  !_input%%~L!
        )else if /i "!_input%%~L:~0,4!" == "www." (
         start "" /b chrome.exe --new-tab "!_input%%~L!" 
        )else if exist "!_input%%~L!\." (
         <con: explorer "!_input%%~L!\."
        )else !_input%%~L!
    )

endlocal && goto :eof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.