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?