简体   繁体   English

综合列表-在批处理文件中运行代码与直接在控制台中输入代码

[英]Comprehensive list - Running code in batch file vs. entering code directly into console

I am looking for a comprehensive list of differences between running the exact same code in the windows cmd windows (entering it manually, line by line) vs. writing a batch file and running that. 我正在寻找在Windows cmd窗口中运行完全相同的代码(逐行手动输入)与编写批处理文件并运行该代码之间的差异的综合列表。

I cannot possibly be the fist person to ask this, but neither Google nor multiple stack overflow searches returned what I wanted. 我可能不是最先问这个问题的人,但是Google和多次堆栈溢出搜索都没有返回我想要的结果。 I mostly get comparison between .bat & .cmd like this one, or specific questions about a single issue . 我主要是比较.bat和.cmd这样的比较,或者有关单个问题的 特定 问题 I know there are other differences, for example i just found out that 我知道还有其他差异,例如我刚刚发现

set "filename=foo"
set "optional_postfix="
set "filetype=.cpp"

set completename=%filename%%optional_postfix%%filetype%
echo %completename%
PAUSE

behave differently. 表现不同。

I would like to read up on all of the differences since finding out about them after trying something for half an hour that should work and finding out about the difference afterwards is pretty annoying. 我想阅读所有差异,因为尝试了半小时后应该能找到答案,然后再找出差异是很烦人的。

If such a list already exists here, please do not downvote me, I actually spent time looking for it and - at least for me - it was not obvious how to find it. 如果这里已经存在这样的列表,请不要对我投票,我实际上是花时间寻找它,并且-至少对我来说-如何找到它并不明显。 Others might have the same issue, so please just mark it as duplicate. 其他人可能有相同的问题,因此请将其标记为重复。

There are numerous differences whether commands are executed in cmd or in batch file context, all of which refer to cmd -internal commands or features though. 有许多不同的命令是否执行cmd或批处理文件背景,所有这些均指cmd -internal命令或虽然功能。

The following commands that do nothing when executed in cmd context (they do not produce any output and do not affect ErrorLevel ): 以下命令在cmd上下文中执行时不执行任何操作(它们不会产生任何输出并且不会影响ErrorLevel ):

  • goto
  • shift
  • setlocal (to en-/disable command extensions or delayed expansion in cmd , invoke the instance using cmd /E:{ON|OFF} /V:{ON|OFF} instead) setlocal (要在cmd启用/禁用命令扩展或延迟扩展,请改用cmd /E:{ON|OFF} /V:{ON|OFF}调用实例)
  • endlocal

Labels (like :Label ) cannot be used in cmd , they are simply ignored. 标签(如:Label )不能在cmd ,它们只会被忽略。 Therefore call :Label cannot be used too (if you do, an error message appears and ErrorLevel becomes set to 1 ). 因此,也无法使用call :Label (如果执行此操作,则会出现错误消息,并且ErrorLevel设置为1 )。

There is of course no argument expansion in cmd since there is no possibility to provide arguments. 当然, cmd不会进行参数扩展 ,因为不可能提供参数。 Hence a string like %1 is just returned as is ( try with echo %1 ). 因此,仅按原样返回类似%1的字符串(尝试使用echo %1 )。 This allows to define variable names in cmd that begin with a numerical digit, which is not possible in batch files (actually you can define them, but you cannot % -expand them; try this in a batch file: set "123=abc" , then set 1 ; you will get the output 123=abc ; but trying to do echo %123% will result in the output 23 , because %1 is recognised as argument reference (given that no argument is supplied)). 这允许在cmd中定义以数字开头的变量名,这在批处理文件中是不可能的(实际上,您可以定义它们,但不能%展开它们;请在批处理文件中尝试以下操作: set "123=abc" ,然后set 1 ;您将获得输出123=abc ;但是尝试执行echo %123%将导致输出23 ,因为%1被识别为参数引用(假设未提供任何参数)。

Undefined environment variables are not expanded (replaced) in cmd but they are maintained literally (do set "VAR=" and echo %VAR% , so you get %VAR% ). 未定义的环境变量不会在cmd展开(替换),而是按字面意义进行维护( set "VAR="echo %VAR% ,这样您就可以获得%VAR% )。 In batch files however, they are expanded to empty strings instead. 但是,在批处理文件中,它们会扩展为空字符串。 Note that in batch files but not in cmd , two consecutive percent signs %% are replaced by a single one % . 请注意,在批处理文件而不是在cmd ,连续两个百分号%%是由一个单一的一个替代% Also in batch files but not in cmd , a single % sign becomes removed. 同样在批处理文件中,但在cmd却没有,单个%符号将被删除。

More information about the percent expansion (both argument references and environment variables) can be found in this thread: How does the Windows Command Interpreter (CMD.EXE) parse scripts? 有关扩展百分比(参数引用和环境变量)的更多信息,可以在此线程中找到: Windows命令解释器(CMD.EXE)如何解析脚本?

The set /A command behaves differently: in cmd it returns the (last) result of an expression on the console (without trailing line-break), but in batch files it does not return anything. set /A命令的行为有所不同:在cmd它返回控制台上表达式的(最后)结果(不带尾随换行符),但是在批处理文件中,它不返回任何内容。

The for command requires its loop variable references to be preceeded by two percent signs (like for %%I in ... ) in batch files, but only a single one (like for %I in ... ) in cmd . for命令要求其循环变量引用在批处理文件中以两个百分号开头(例如, for %%I in ... ),但在cmd仅一个(例如, for %I in ... )。 This is a consequence of the different percent expansion handling in batch files and in cmd . 这是批处理文件和cmd扩展处理百分比不同的结果。

Finally, some commands do not reset the ErrorLevel upon success, unless they are used within a batch file with .cmd extension. 最后,除非成功在带有.cmd扩展名的批处理文件中使用某些命令,否则它们不会在成功时重置ErrorLevel These are: 这些是:

  • assoc
  • dpath
  • ftype
  • path
  • prompt
  • set

More information about resetting the ErrorLevel can be found in this post: Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success? 有关重置ErrorLevel更多信息,可以在这篇文章中找到: 哪些cmd.exe内部命令在成功后将ERRORLEVEL清除为0?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM