BATCH用户输入设置为变量,在脚本结束时检查是否存在并应用命令

时间:2021-05-18 23:21:57

Should be straight forward I think, well I thought it was, but having some trouble getting it to work.

应该是直截了当的,我想,我认为是这样,但是让它运转起来有些麻烦。

Aims:- Create automated install using various batch commands, files and software installers. In order that the user doesn't have to complete the install, a set it and forget it affair, I want them to choose from 3 options at the start of the process BUT only get applied at the end, so the one of the 3 tasks they chose is done without user input.

目标: - 使用各种批处理命令,文件和软件安装程序创建自动安装。为了使用户不必完成安装,设置它并忘记它的事情,我希望它们在过程开始时从3个选项中选择但是只在最后应用,所以3中的一个他们选择的任务是在没有用户输入的情

It's all done just having problems with the start phase.

这一切都只是在启动阶段遇到问题。

Code: User can choose 3 things, copy a file and start the software, don't copy a file and launch a config tool, copy and file and don't run the software (waiting on additional prep).

代码:用户可以选择3件事,复制文件并启动软件,不复制文件,启动配置工具,复制和存档,不运行软件(等待额外准备)。

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set end1=end1
if '%Input%'=='2' set end2=end2
if '%Input%'=='3' set end3=end3

Here is the start menu above, as you can see the number choice 'should' be setting a variable. After this is set, the file runs, installs and does things, then comes to the end where it should run the corresponding action defined by the start menu choice So...

这是上面的开始菜单,因为您可以看到数字选项'应该'设置变量。设置完成后,文件运行,安装并执行操作,然后到达应该运行开始菜单选项定义的相应操作的结束。

IF exist %end1% GOTO end1
IF exist %end2% GOTO end2
IF exist %end3% GOTO end3

However it never detects anything other than just running the first of the choices by default. Ideally if the client pressed 1,2 or 3 at the beginning, when it comes the end, it goes to the menu option related where the appropriate action is launched;

但是,除了默认运行第一个选项之外,它永远不会检测到任何其他内容。理想情况下,如果客户端在开头按下1,2或3,当它结束时,它会转到与启动相应操作相关的菜单选项;

:end 1 "copies a file" launches software
:end 2 "starts a program"
:end 3 "copies a file in preparation for some other task"

Hope that makes sense. Let me know if you need more info. I have searched but probably not searching for the right thing.

希望有道理。如果您需要更多信息,请告诉我。我搜索过但可能没有找到正确的东西。

If I try and echo for the presence of end1 and it says it is not defined. I'm guessing maybe error level checks would be better, but struggling with that also.

如果我尝试回显end1的存在,它说它没有定义。我猜测错误级别检查可能会更好,但也会挣扎。

Thank you...

3 个解决方案

#1


0  

EXIST checks for the existence of a file or folder. You want DEFINED. http://ss64.com/nt/if.html

EXIST检查文件或文件夹是否存在。你想要DEFINED。 http://ss64.com/nt/if.html

I'm not sure if you really need to set 3 different variables though (maybe you've done it this way to reduce the issue to simplest steps). You can restructure it a bit like this:

我不确定你是否真的需要设置3个不同的变量(也许你这样做是为了将问题简化为最简单的步骤)。你可以重新调整它有点像这样:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)

:end1
echo ending 1
pause

:end2
echo ending 2
pause

:end3
echo ending 3
pause

This code works for me.

这段代码适合我。

#2


0  

OK been fiddling, this works:

好的,小提琴,这工作:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3

~things happen~

color 1f
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)

All three menu options behave!

所有三个菜单选项都表现出来!

#3


-1  

Assuming that end1, end2, end3 are defined elsewhere in the batch as folder paths, try this:

假设end1,end2,end3在批处理中的其他位置定义为文件夹路径,请尝试以下操作:

@echo off
set "mes1=Enter a Number Choice and Press RTN"
choice /c 123 /m "%mes1%" /t 10 /d 1
if %errorlevel% equ 3 (set disk=end3
) else if %errorlevel% equ 2 (set "disk=end2
) else (set disk=end1)

if exist "%disk%" (
   if %disk%==%end1% (goto :end_1
   ) else if %disk%==%end2% (goto :end_2
   ) else if %disk%==%end3% (goto :end_3)
::some code here

:end_1 rem "copies a file" launches software
:end_2 rem "starts a program"
:end_3 rem "copies a file in preparation for some other task"

If you don't use drive paths, try this:

如果您不使用驱动器路径,请尝试以下操作:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' (set ending=1
) else if '%Input%'=='2' (set ending=2
) else if '%Input%'=='3' (set ending=3)
if defined ending (GOTO :process%ending%) else (goto :end)
:: put some code here

:process1 rem "copies a file" launches software
:process2 rem "starts a program"
:process3 rem "copies a file in preparation for some other task"

:end
timeout 5 >nul
exit /b

#1


0  

EXIST checks for the existence of a file or folder. You want DEFINED. http://ss64.com/nt/if.html

EXIST检查文件或文件夹是否存在。你想要DEFINED。 http://ss64.com/nt/if.html

I'm not sure if you really need to set 3 different variables though (maybe you've done it this way to reduce the issue to simplest steps). You can restructure it a bit like this:

我不确定你是否真的需要设置3个不同的变量(也许你这样做是为了将问题简化为最简单的步骤)。你可以重新调整它有点像这样:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)

:end1
echo ending 1
pause

:end2
echo ending 2
pause

:end3
echo ending 3
pause

This code works for me.

这段代码适合我。

#2


0  

OK been fiddling, this works:

好的,小提琴,这工作:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' set ending=1
if '%Input%'=='2' set ending=2
if '%Input%'=='3' set ending=3

~things happen~

color 1f
IF %ending% EQU 1 GOTO end1
IF %ending% EQU 2 (GOTO end2) ELSE (GOTO end3)

All three menu options behave!

所有三个菜单选项都表现出来!

#3


-1  

Assuming that end1, end2, end3 are defined elsewhere in the batch as folder paths, try this:

假设end1,end2,end3在批处理中的其他位置定义为文件夹路径,请尝试以下操作:

@echo off
set "mes1=Enter a Number Choice and Press RTN"
choice /c 123 /m "%mes1%" /t 10 /d 1
if %errorlevel% equ 3 (set disk=end3
) else if %errorlevel% equ 2 (set "disk=end2
) else (set disk=end1)

if exist "%disk%" (
   if %disk%==%end1% (goto :end_1
   ) else if %disk%==%end2% (goto :end_2
   ) else if %disk%==%end3% (goto :end_3)
::some code here

:end_1 rem "copies a file" launches software
:end_2 rem "starts a program"
:end_3 rem "copies a file in preparation for some other task"

If you don't use drive paths, try this:

如果您不使用驱动器路径,请尝试以下操作:

SET /P "Input=Enter a Number Choice & Press RTN: "
if '%Input%'=='1' (set ending=1
) else if '%Input%'=='2' (set ending=2
) else if '%Input%'=='3' (set ending=3)
if defined ending (GOTO :process%ending%) else (goto :end)
:: put some code here

:process1 rem "copies a file" launches software
:process2 rem "starts a program"
:process3 rem "copies a file in preparation for some other task"

:end
timeout 5 >nul
exit /b