write .bat file and execute command based on input - windows

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

do some other task
START
START
START 

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

input 2 launch only

input 3 launch only

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

3 Answers

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

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
IF %input% == 5 START
IF %input% == 6 cmd /k "cd changefolder"
IF %input% == 7 call another.bat

Final thing launching with keyboard-shortcutOne 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
IF %input% == 5 START
IF %input% == 6 cmd /k "cd changefolder"
IF %input% == 7 call another.bat
2

// 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 "" "" Option 6 = start "" "" 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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like