我在 Windows 中编写了一个.bat
文件。在浏览器中启动这些 URL
do some other task
START www.google.com
START www.microsoft.com
START www.apple.com
但是我写的是静态的,它们一起执行。
我想让它接受输入并根据输入执行该命令。
例如
1
仅输入启动www.google.com
2
仅输入启动www.microsoft.com
3
仅输入启动www.apple.com
输入4
执行其他任务或运行命令
还希望.bat
使用相关的键盘快捷键打开启动文件。
如果有关系的话我会用它Windows 10
任何帮助,将不胜感激。
谢谢
答案1
可能是这样的:
@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
答案2
经过一番搜索后我终于能够做到这一点。
基本上要接受.bat
文件输入你需要这个
set /P input="What you gonna do? "
然后可以将其用作变量
@echo Hello %input%!
为了比较输入的值,我们可以使用IF
语句并使其执行所需的命令
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
最后一件事是启动keyboard-shortcut
不能.bat
直接将键盘快捷键分配给文件。但你可以分配给Shortcut
。为此
- 右键单击
.bat
文件并单击Create Shortcut
- 右键单击新创建的快捷方式。转到
Properties
。输入所需的键作为快捷方式。单击确定
轰隆隆,你就可以出发了。
所以我的整个.bat
文件看起来像这样
@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
答案3
// Edit
检查输入并运行命令的一些选项:
@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"
观察:使用spaceOption 1 = command
Edit //
您可以使用从 1 到 5 的一个循环来定义您的输入,set /p
并使用if
条件来检查您是否要使用资源管理器打开文件夹、启动浏览器或运行命令......
@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