我想创build一个windows批处理脚本,让用户一次input多个选项,然后程序运行。
参考这个网站( 批量文件上的多个选项菜单? ),我开始知道它的作用是允许多个select。 但是,这是在bash脚本。 例如…
@echo off setlocal enabledelayedexpansion echo Which would you like to use? echo 1. Hello.txt echo 2. Byebye.txt echo 3. ThisIsText.txt echo 4. MyBatchScript.txt echo 5. All set /p op=Type the numbers of the names you want to use (separated by commas with no spaces. Eg: 1,3,2):
在此之前,它通过提示用户select一个或多个选项来工作。
for /f "delims=, tokens=1-5" %%i in ("op") do ( set i=%%i set j=%%j set k=%%k set l=%%l set m=%%m )
然而,直到这里,我意识到select将被存储到一个variables“操作”,这将是在i
。 基本上,不使用j
, k
, l
和m
。 我不确定我是否解释错了。 希望我没有错误地解释编码。
所以为了我想要的是…
当用户只select一个选项时,它会把“Hello.txt”插入命令(例如)
echo This is complicated > Hello.txt
但是,如果用户select多个选项(例如,用户input1,2),则将插入
echo This is complicated > Hello.txt echo This is complicated > Byebye.txt
如果用户select选项'5',则不能与其他号码一起input(因为它是全部的)。 然后它会回显This is complicated > Byebye.txt
, Hello.txt
等
无论如何,使用批处理脚本来做到这一点?
编辑:任何人都可以解释这个给我? 我试图find不同的网站,但我仍然不明白。 对不起,我是编写批处理脚本的新手。 所以对它的理解还不深刻。 免责声明:这是我从上面提到的网站获得的编码。
if %i%X neq X set last=1b & goto %i% :1b if %j%X neq X set last=2b & goto %j% :2b if %k%X neq X set last=3b & goto %k% :3b if %l%X neq X set last=4b & goto %l% :4b if %m%X neq X set last=%m% & goto %m% goto next :1 ::Put the code for doing the first option here goto %last% :2 ::Put the code for doing the second option here goto %last% :3 ::Put the code for doing the third option here goto %last% :4 ::Put the code for doing the fourth option here goto %last% :5 ::Put the code for doing the fifth option here goto %last%
我不明白这有助于运行多个命令。 如果我在这个领域input了1,2,3
,那么我怎样才能把它放在一起呢?
您可以充分利用以下事实:flat FOR命令中的标准分隔符(no / F选项)是空格,逗号,分号和等号:
@echo off setlocal enabledelayedexpansion echo Which would you like to use? echo 1. Hello.txt echo 2. Byebye.txt echo 3. ThisIsText.txt echo 4. MyBatchScript.txt echo 5. All :getOptions set /p "op=Type the numbers of the names you want to use (separated by commas OR spaces): " if "%op%" equ "" goto getOptions if %op% equ 5 set op=1,2,3,4 for %%a in (%op%) do ( echo Process option %%a call :option-%%a ) goto :EOF :option-1 echo 1. Hello.txt exit /B :option-2 echo 2. Byebye.txt exit /B :option-3 echo 3. ThisIsText.txt exit /B :option-4 echo 4. MyBatchScript.txt exit /B
你写道,你试过('%op'), ("%op"), (%op)
和变化。
它应该是:( ("%op%")
只有变量和命令行参数使用<Percent><char>
语法。
op
是一个“正常”变量,它用于<Percent><name><Percent>
: %op%