如何在控制台窗口中创建带有bat文件/批处理脚本的清单?

时间:2016-01-21 22:26:43

标签: windows batch-file console

我希望在控制台窗口中创建一个清单,以帮助用户选择他们想要安装的某些选项。这种输出:

Please select options to install:
[x]Option 1
[ ]Option 2
>[x]Option 3
[x]Option 4

用户可以在整个列表中移动光标并选择选项。

我对如何执行此代码有一个非常非常模糊的想法,仅使用两个选项进行测试。但是,如果有人已经对这将如何运作并且可以分享我已经有了一个凝固的想法,我将非常感激!

为想要查看它的人测试代码:

@echo off

call:main
end /b 0

:main
set /p choice= "Would you like to enable option one? (yes/no): "echo(
if %choice%==yes (
    set option1=1
)
if %choice%==no (
    set option1=0
)
call:mod1
set /p choice= "Would you like to enable option two? (yes/no): "echo(
if %choice%==yes (
    set option2=1
)
if %choice%==no (
    set option2=0
)
call:mod2

:mod1
if %option1%==1 (
echo [x] Option 1
)
if %option1%==0 (
echo [ ] Option 1
)

:mod2
if %option2%==1 (
echo [x] Option 1
)
if %option2%==0 (
echo [ ] Option 1
)

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

@echo off
setlocal EnableDelayedExpansion

for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set "getKeyMacro=powershell -noprofile "^
    while (-not (37..40+13).contains($x)) {^
        $x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
    }^
    if ($x -eq 13) {^
    'enter'^
    }^
    ('left','up','right','down')[$x - 37]^
""

set "option1=0"
set "option2=0"
set "option3=0"
set "option4=0"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,4) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! Option %%G
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr 4 set "selected=4"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select

:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select

:OK
FOR /L %%G IN (1,1,4) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause

请注意,这在很大程度上取决于延迟扩展,因此您可能需要阅读here

第二个注意事项:这需要PowerShell,因此您可以使用向上和向下箭头键选择选项,输入以切换当前选定的选项,使用右箭头继续。

修改

更新版本,这允许您设置选项的显示名称,但您还需要指定选项的数量:

@echo off
setlocal EnableDelayedExpansion

for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set "getKeyMacro=powershell -noprofile "^
    while (-not (37..40+13).contains($x)) {^
        $x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
    }^
    if ($x -eq 13) {^
    'enter'^
    }^
    ('left','up','right','down')[$x - 37]^
""

set "option1=0"
set "option2=0"
set "option3=0"
set "option4=0"
set "option1name=Install thing 1"
set "option2name=Do thing 2"
set "option3name=Execute thing 3"
set "option4name=Run thing 4"
set "maxOptions=4"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,%maxOptions%) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! !option%%Gname!
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr %maxOptions% set "selected=!%maxOptions%!"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select

:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select

:OK
FOR /L %%G IN (1,1,%maxOptions%) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause

编辑#2

现在使用@Aacini的for循环启动变量,所以这只需要发生一次,因此不再需要手动maxoption:

@echo off
setlocal EnableDelayedExpansion

set "getKeyMacro=powershell -noprofile "^
    while (-not (37..40+13).contains($x)) {^
        $x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
    }^
    if ($x -eq 13) {^
    'enter'^
    }^
    ('left','up','right','down')[$x - 37]^
""

set "num=0"
for %%a in ("Install thing 1"
            "Do thing 2"
            "Execute thing 3"
            "Run thing 4") do (
   set /A num+=1
   set "option!num!=0"
   set "option!num!name=%%~a"
)
set "maxOptions=%num%"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,%maxOptions%) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! !option%%Gname!
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr %maxOptions% set "selected=!%maxOptions%!"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select

:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select

:OK
FOR /L %%G IN (1,1,%maxOptions%) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause

答案 1 :(得分:0)

$url = 'https://graph.facebook.com/274084006029120/picture?width=100';

file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)\n/', implode("\n", $http_response_header), $matches);

if (isset($matches[0]))
{
    echo "<img src='".matches[0]."' />";
}