0

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.

Thomas
  • 4,696
  • 5
  • 36
  • 71
  • 1
    At first, reference this thread: [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133) – aschipfl Jun 14 '17 at 13:41
  • Quick reply out of my head: some commands do not work in `cmd`: `goto`, `shift`, `setlocal`, `endlocal`, and `call :Label`; ; `set /A` writes the (last) result to console in `cmd` but not in batch files; there is no [argument expansion](http://ss64.com/nt/syntax-args.html); undefined environment variables expand to empty strings in batch files but are maintained literally in `cmd`; `%%` become a literal `%` in batch files but not in `cmd`; that is why you have to state `for %%I in ...` in batch files, but `for %I in ...` in `cmd`; and a few commands may or may not reset `ErrorLevel`... – aschipfl Jun 14 '17 at 13:46
  • @aschipfl: Thanks for the link, this seems to be a good start. No wonder I didn't find it by searching, I would never have guessed it contains what I am looking for just by reading the title. – Thomas Jun 14 '17 at 14:10
  • 1
    Although the topic is different, you may review a list of these differences and whole examples of how to use such command-line commands _not_ in a Batch file at [this answer](https://stackoverflow.com/questions/13320578/how-to-run-batch-script-with-out-using-bat-extension/13337597#13337597) – Aacini Jun 14 '17 at 16:32

1 Answers1

1

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99