简体   繁体   中英

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.

I cannot possibly be the fist person to ask this, but neither Google nor multiple stack overflow searches returned what I wanted. I mostly get comparison between .bat & .cmd like this one, or specific questions about a single issue . 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.

The following commands that do nothing when executed in cmd context (they do not produce any output and do not affect 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)
  • endlocal

Labels (like :Label ) cannot be used in cmd , they are simply ignored. Therefore call :Label cannot be used too (if you do, an error message appears and ErrorLevel becomes set to 1 ).

There is of course no argument expansion in cmd since there is no possibility to provide arguments. Hence a string like %1 is just returned as is ( try with 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)).

Undefined environment variables are not expanded (replaced) in cmd but they are maintained literally (do set "VAR=" and echo %VAR% , so you get %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 % . Also in batch files but not in cmd , a single % sign becomes removed.

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?

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.

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 . This is a consequence of the different percent expansion handling in batch files and in cmd .

Finally, some commands do not reset the ErrorLevel upon success, unless they are used within a batch file with .cmd extension. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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