一批中调用批处理? [英] Call batch within batch?

查看:148
本文介绍了一批中调用批处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何调用一个批次内的多个批处理文件?当我尝试它总是去同一个或根本没有,并关闭窗口。

 关闭@echo
:菜单
标题MENU0
回声1 - 选择菜单1
回音2 - 选择菜单2
呼应0 - 退出
回声。
SET / p =选择输入你想要的数量或任务的字母,然后preSS ENTER键:
IF%选择%== 1 GOTO 1
IF%选择%== 2 GOTO 2
IF%选择%== 0 EXIT:1
打电话%USERPROFILE%\\ \\\\ Menu1.bat桌面:2
打电话%USERPROFILE%\\桌面\\ Menu2.bat


解决方案

有与问题提供的批处理code的几个问题。

第一种是调用命令拨打该批处理文件的处理后完成,当前批处理文件的处理下一个命令分别行继续,除了被称为批处理文件包含本身的命令退出不带参数 / B 在这种情况下,命令处理器终止本身独立于调用层次。

有关详细信息拨打行为上看到的答案:

第二个问题是分配给环境变量文件夹路径 USERPROFILE 可以包含一个或多个空格(默认情况下在Windows 2000 / XP,就可能根据用户名以后的Windows版本)。因此,始终附上双引号括起来引用 USERPROFILE USERNAME

第三个也是最难以处理的问题是,在提示一个批处理文件的用户提供设置/ P 可以自由地输入任何内容,而不是作家正是的批处理文件建议。

例如

  SET / p =选择输入你想要的数量或任务的字母,然后preSS ENTER键:
IF%选择%== 1 GOTO 1

结果引起的语法错误批处理的出口,如果该批次用户点击刚刚<大骨节病>返回或<大骨节病> ENTER 不输入的所有东西和环境变量选择不是一个有用的字符串,因为在这种情况下,已确定的下一行由命令处理器处理是:

  IF == 1 GOTO 1

这是很好的做法之前设置/ P 当批用户只需点击此值保持<大骨节病>返回或<大骨节病> ENTER

一个批量用户也使用设置自由/ P 进入任何东西,包括像语法关键字符&LT; | &GT; 及其他由错误或故意(由一个语法错误破批处理)。

因此​​,这是一般更好地为在批处理文件菜单使用命令选择微软的文章),因为该批次的用户只能进入什么样的批处理文件提供的作家。但 CHOICE 只是默认情况下,Windows Server 2003和以后的Windows可用。并有不同的版本选择的( SS64 的文章提供更多的信息)用一组不同的选项。所以,这取决于批处理文件是专为如 CHOICE 可以使用在所有的Windows版本(S)。

这也是不好的命名一个环境变量或类似,虽然可能一个命令的标签。因此,不是一个环境变量一个好名字。

下面是一个code避免了所有这些问题的评论批处理文件。

 关闭@echo
:主菜单
SETLOCAL EnableDelayedExpansion
标题MENU0
CLS
回声1 - 选择菜单1
回音2 - 选择菜单2
呼应0 - 退出
回声。在用户的情况下,物权定义0作为默认值只是击中RETURN或ENTER键。
集UsersChoice = 0
集/ PUsersChoice =输入你想要的,那么preSS输入号码或任务的信:REM有用户真正刚进入所提供的人物之一?REM必须有没有要处理,如果用户刚刚进入0
REM或1或2。否则用户的选择是或者错误或
REM故意进入错误的。由用户输入的字符串是
延迟扩展引用物权,避免批量出口
在用户的情况下,REM处理进入了一个关键的语法字符。FOR / F标记= 1 delims = 012%%我在(!UsersChoice!)做(
    ENDLOCAL
    后藤的MainMenu
)REM现在是安全的引用变量的值,而不使用延迟
REM扩张所造成的用户输入的不能再出现语法错误。REM输入的字符串不包含任何不期望的字符。但
REM是可能的,例如11被错误地输入,而不是
只是1.输入的字符串的物权应该有1个字符的长度。如果不是%UsersChoice:〜1,1%==(
    ENDLOCAL
    后藤的MainMenu
)REM退出用户这个批处理进入0 previous环境
REM自动命令处理器通过一个隐含的ENDLOCAL恢复。如果%UsersChoice%==0退出/ BREM恢复previous环境为称为批处理文件最
REM可能使用标准的命令环境编写与延迟
REM扩展未启用(感叹号间preTED不同)。
REM局部环境变量的当前值必须传递
REM为在GOTO命令的用法previous环境。ENDLOCAL&安培;转到菜单%UsersChoice%:菜单1
所谓的%USERPROFILE%\\桌面\\ Menu1.bat
后藤的MainMenu:菜单2
所谓的%USERPROFILE%\\桌面\\ Menu2.bat
后藤的MainMenu

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。


  • 呼叫/?

  • CLS /?

  • 回声/?

  • ENDLOCAL /?

  • 退出/

  • 为/?

  • 转到/?

  • REM /?

  • 设置/?

  • SETLOCAL /?

  • 标题/?

有关&放的意思; 的行 ENDLOCAL&安培;在看答案href=\"http://stackoverflow.com/a/25344009/3074564\">使用Windows批处理文件多个命令单线。

How do I call multiple batch files within a single batch? When I try it always goes to the same one or none at all and closes window.

@echo off
:MENU
title MENU0
Echo 1 - Select Menu 1
Echo 2 - Select Menu 2
Echo 0 - Exit
Echo.


SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1
IF %choice%==2 GOTO 2
IF %choice%==0 EXIT

:1
call %userprofile%\desktop\\Menu1.bat

:2
call %userprofile%\desktop\Menu2.bat

解决方案

There are several issues with provided batch code in question.

The first one is that after processing of the batch file called with command CALL finished, the processing of current batch file continues with the next command respectively line, except the called batch file contains itself the command EXIT without parameter /B as in this case the command processor terminates itself independent on calling hierarchy.

For details about CALL behavior see answers on:

The second issue is that folder path assigned to environment variable USERPROFILE could contain 1 or more spaces (default on Windows 2000/XP, possible on later Windows versions depending on user name). Therefore always enclose a string referencing USERPROFILE or USERNAME in double quotes.

The third and most difficult to handle issue is that the user of a batch file on prompt with set /P has the freedom to enter anything and not just what the writer of the batch file suggests.

For example

SET /P choice=Type the number or letter of task you want, then press ENTER:
IF %choice%==1 GOTO 1

results in an exit of batch processing caused by a syntax error if the batch user hits just RETURN or ENTER without entering anything at all and the environment variable choice is not already defined with a useful string because in this case the next line to process by command processor is:

IF ==1 GOTO 1

It is good practice to define the environment variable with a default value before set /P as this value is kept when the batch user just hits RETURN or ENTER.

A batch user has also the freedom on using set /P to enter anything including syntax critical characters like " or < or | or > and others by mistake or intentionally (for breaking batch processing by a syntax error).

Therefore it is in general better for menus in batch files to use the command choice (Microsoft article) because then the batch user can enter only what the writer of the batch file offers. But CHOICE is available only by default for Windows Server 2003 and later Windows. And there are different versions of choice (SS64 article with additional information) with a different set of options. So it depends on which Windows version(s) the batch file is designed for if CHOICE can be used at all.

It is also not good to name an environment variable or a label like a command although possible. Therefore choice is not a good name for an environment variable.

Here is a commented batch file with a code which avoids all those issues.

@echo off
:MainMenu
setlocal EnableDelayedExpansion
title MENU0
cls
echo 1 - Select Menu 1
echo 2 - Select Menu 2
echo 0 - Exit
echo.

rem Define 0 as default value in case of user just hits RETURN or ENTER.
set "UsersChoice=0"
set /P "UsersChoice=Type the number or letter of task you want, then press ENTER: "

rem Has the user really entered just one of the offered characters?

rem There must be nothing to process if the user has entered just 0
rem or 1 or 2. Otherwise the user's choice was either by mistake or
rem intentionally entered wrong. The string entered by the user is
rem referenced with delayed expansion to avoid an exit of batch
rem processing in case of user entered a syntax critical character.

for /F "tokens=1 delims=012" %%I in ("!UsersChoice!") do (
    endlocal
    goto MainMenu
)

rem Now it is safe to reference the variable value without usage of delayed
rem expansion as a syntax error caused by user input can't occur anymore.

rem The entered string does not contain any not expected character. But
rem it is possible that for example 11 was entered by mistake instead
rem of just 1. The entered string should have a length of 1 character.

if not "%UsersChoice:~1,1%" == "" (
    endlocal
    goto MainMenu
)

rem Exit this batch processing on user entered 0. Previous environment is
rem automatically restored by command processor by an implicit endlocal.

if "%UsersChoice%" == "0" exit /B

rem Restore previous environment as the called batch files are most
rem likely written for using standard command environment with delayed
rem expansion not enabled (exclamation mark interpreted different).
rem The current value of local environment variable must be passed
rem to previous environment for usage on GOTO command.

endlocal & goto Menu%UsersChoice%

:Menu1
call "%USERPROFILE%\Desktop\Menu1.bat"
goto MainMenu

:Menu2
call "%USERPROFILE%\Desktop\Menu2.bat"
goto MainMenu

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • cls /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

For meaning of & in line endlocal & goto Menu%UsersChoice% see answer on Single line with multiple commands using Windows batch file.

这篇关于一批中调用批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆