Difference between .cmd & .bat files?

5275 views
Skip to first unread message

Gregory Mamayek

unread,
Jul 18, 2003, 1:40:39 PM7/18/03
to
Gang:\>

How come some files fire using .cmd, while others use .bat?
When would you use either one?

Is it because .cmd is for execution, while .bat is used to provide a
data source (ie. like web page .htm using SQL to call data source .bat)?

--
Cordially,
Greg
A+ BS MCSE Web Developer


Gregory Mamayek

unread,
Jul 18, 2003, 1:46:01 PM7/18/03
to
Gang:\>

I looked at the extensions, so I see the different properties.

Also, is debug a good way to get more information about what's going on
compared to the last result: 0x0 field in scheduled tasks?

Dean Wells [MVP]

unread,
Jul 18, 2003, 1:50:12 PM7/18/03
to
No difference in behavior ... whatsoever ... in the context of this
newsgroup at least.

Legacy extension implying batch of commands = .BAT
Uplevel extension implying shell script = .CMD

.BAT = common to x-DOS operating systems
.CMD = used by OS/2 and introduced in Windows NT4

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Gregory Mamayek" <gmam...@dllr.state.md.us> wrote in message
news:3F183117...@dllr.state.md.us...

Gregory Mamayek

unread,
Jul 18, 2003, 2:15:13 PM7/18/03
to
Dean:

Thanx for reply.

I am running WIN2K, but I do see a difference in that the .cmd's work,
while the .bat's don't. If I understand you correctly, this may be due to
the OS, or maybe it's the commands I'm using within the batch files.

I will have to play with some more examples to examine their behavior.

The best example is Ritchie's HTML CMD tool. It didn't work when I named
the batch file with .bat, but worked when I used .cmd.

Just a newbie...LOL

Dean Wells [MVP]

unread,
Jul 18, 2003, 3:16:52 PM7/18/03
to
I've not seen Richie's script, can you post it?

I've discovered no differences in the behavior between .BAT and .CMD
from Windows 2000 onwards.

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Gregory Mamayek" <gmam...@dllr.state.md.us> wrote in message

news:3F183930...@dllr.state.md.us...

Ritchie

unread,
Jul 18, 2003, 3:37:20 PM7/18/03
to
"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in message news:#v1wLUVT...@tk2msftngp13.phx.gbl...

> No difference in behavior ... whatsoever ... in the context of this
> newsgroup at least.

I don't mean to be picky, but that's not strictly true. Create two copies
of the script below, save one as a .bat, the other as a .cmd:-

@echo off&setlocal ENABLEEXTENSIONS
call :func&&echo/I'm a cmd||echo/I'm a bat
goto :EOF

:func
md;2>nul
set var=1

I think .cmd is the preferred extension for NT/2000/XP/2003 scripts.

--
Ritchie

NuT CrAcKeR

unread,
Jul 18, 2003, 4:07:56 PM7/18/03
to
Since no one else has added this to the mix... here it goes.

.bat extetions are executed in a 16bit VDM while .cmd extentions are
executed in a 32bit VDM. This has implications for scripts that may need to
access shared memory from other processes or applications.

.... this is an addendum to what the others in the group have already said.

NuTs

"Gregory Mamayek" <gmam...@dllr.state.md.us> wrote in message
news:3F183117...@dllr.state.md.us...

Dean Wells [MVP]

unread,
Jul 18, 2003, 4:42:38 PM7/18/03
to
I'm afraid that's not the case. Binaries that require a 16 bit
environment are identified by the OS at runtime, an NTVDM is spawned and
the binary continues (most of the time) within that process. The
extension used by a shell script (BAT or CMD) does not trigger any
further processes to be spawned and, in my experience, causes no
behavioral differences when executing ... though I'm eagerly awaiting a
script from Richie to prove me wrong. :)

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"NuT CrAcKeR" <nutcr...@intenationalhacket.org> wrote in message
news:Ows13gWT...@TK2MSFTNGP10.phx.gbl...

Dean Wells [MVP]

unread,
Jul 18, 2003, 4:43:49 PM7/18/03
to
Hi Richie,

I've tested your enclosed syntax on 2000, XP and 2003. The bahavior of
the .BAT script is is identical to that of the .CMD script on all 3
platforms ... unless I'm missing the difference you're attempting to
exhibit.

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Ritchie" <qiournvdlirh...@hotmail.com> wrote in message
news:bf9iad$cgcjq$1...@ID-156657.news.uni-berlin.de...

Dean Wells [MVP]

unread,
Jul 18, 2003, 4:56:07 PM7/18/03
to
I stand corrected ... I've just this seond noticed the difference
(opening my eyes must have helped ;)

That's an interesting behavioral difference ... I'll do some digging and
see what else turns up.

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in message

news:eELLM1WT...@tk2msftngp13.phx.gbl...

Dean Wells [MVP]

unread,
Jul 18, 2003, 5:35:02 PM7/18/03
to
It appears to require 3 components (at least that I've found so far) in
order to trigger the behavioral difference -

1. Use of a call statement
2. An error of some kind
3. an environment modification

Does this concur with your own findings?

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

"Ritchie" <qiournvdlirh...@hotmail.com> wrote in message
news:bf9iad$cgcjq$1...@ID-156657.news.uni-berlin.de...

Ritchie

unread,
Jul 19, 2003, 5:16:52 AM7/19/03
to
"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in message news:OZn$zRXTDH...@TK2MSFTNGP12.phx.gbl...

> It appears to require 3 components (at least that I've found so far) in
> order to trigger the behavioral difference -
>
> 1. Use of a call statement
> 2. An error of some kind
> 3. an environment modification

It's simply that a .cmd file using the SET command to assign a value to
a variable, results in the errorlevel being cleared (set to zero). Doing
the same in a .bat file does not change the errorlevel. See this batch
file, and the output below for an example:-

------------ a.bat/a.cmd starts below ------------------
@echo off & setlocal ENABLEEXTENSIONS
ver>nul
echo/After the VER command, errorlevel is %errorlevel%
md;2>nul
echo/Using MD incorrectly, errorlevel is %errorlevel%
set var=1
echo/After setting var, errorlevel is %errorlevel%
------------ a.bat/a.cmd ends above --------------------

<SCREEN SHOT>

d:\data\bat\help>a.cmd
After the VER command, errorlevel is 0
Using MD incorrectly, errorlevel is 1
After setting var, errorlevel is 0

d:\data\bat\help>a.bat
After the VER command, errorlevel is 0
Using MD incorrectly, errorlevel is 1
After setting var, errorlevel is 1

<\SCREEN SHOT>

--
Ritchie


Dean Wells [MVP]

unread,
Jul 19, 2003, 8:20:38 AM7/19/03
to
Appreciate it.

Do you know of any further handling differences?

PS - Out of interest, did you just stumble across this one or was it
documented somewhere?

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]

R a m o v e t h e m a s k t o s e n d e m a i l


"Ritchie" <qiournvdlirh...@hotmail.com> wrote in message

news:bfb2a6$cv8fu$1...@ID-156657.news.uni-berlin.de...

Tim Hill

unread,
Jul 20, 2003, 9:26:07 PM7/20/03
to
There are NO differences between a .BAT and a .CMD script when executed on a
platform that understands the .CMD file type (NT et al). If I recall
correctly, the .CMD type was inherited from OS/2.

One common "urban myth" is that .BAT files are somehow run in a 16-bit
NTVDM. Not so, they are tun by CMD.EXE just like .CMD files.

The only real use of the two file types is to prevent backward
compatibility. If I write a script that uses NT-only features (e.g. "FOR
/D") then by naming it .CMD I prevent anyone accidentally running it on a
Win9x box.

-Tim

"Gregory Mamayek" <gmam...@dllr.state.md.us> wrote in message
news:3F183117...@dllr.state.md.us...

Phil Robyn

unread,
Jul 20, 2003, 9:40:06 PM7/20/03
to
Tim Hill wrote:

> There are NO differences between a .BAT and a .CMD script when executed on a
> platform that understands the .CMD file type (NT et al). If I recall
> correctly, the .CMD type was inherited from OS/2.
>
> One common "urban myth" is that .BAT files are somehow run in a 16-bit
> NTVDM. Not so, they are tun by CMD.EXE just like .CMD files.
>
> The only real use of the two file types is to prevent backward
> compatibility. If I write a script that uses NT-only features (e.g. "FOR
> /D") then by naming it .CMD I prevent anyone accidentally running it on a
> Win9x box.
>
> -Tim
>

Are you *the* Tim Hill (of _Windows NT Shell Scripting_ fame)?

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Dean Wells [MVP]

unread,
Jul 20, 2003, 9:56:46 PM7/20/03
to
RE: "NO differences" - An opinion with which I would have recently
agreed ... please note the earlier posts within this thread, most
specifically those from Ritchie.

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]

R e m o v e t h e m a s k t o s e n d e m a i l


"Tim Hill" <nos...@span.com> wrote in message
news:%23wSoScy...@TK2MSFTNGP12.phx.gbl...

Ritchie

unread,
Jul 21, 2003, 2:14:35 AM7/21/03
to
"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in message news:umS$rAfTDH...@TK2MSFTNGP10.phx.gbl...

> Do you know of any further handling differences?

No, that's the only difference I'm aware of.

> PS - Out of interest, did you just stumble across this one or was it
> documented somewhere?

It was stumbled upon in the 'hangman' script. I think it was Garry D/Musafir
that actually pin-pointed the issue. See this (longish) thread:-

Subject: Minimise DOS prompts
Newsgroups: microsoft.public.win2000.cmdprompt.admin
Date: 2003-04-16 10:37:41 PST
http://groups.google.com/groups?threadm=04d601c3043e%2492ac6180%242f01280a%40phx.gbl

Looks like Tim Hill might have to revise his book, LOL

--
Ritchie

Mark Zbikowski (MSFT)

unread,
Jul 21, 2003, 12:18:47 PM7/21/03
to
The differences between .CMD and .BAT as far as CMD.EXE is concerned are:

With extensions enabled, PATH/APPEND/PROMPT/SET/ASSOC in .CMD
files will set ERRORLEVEL regardless of error. .BAT sets ERRORLEVEL
only on errors.


--
Disclaimer: This posting is provided "AS IS" with no warranties, and confers
no rights.


"Gregory Mamayek" <gmam...@dllr.state.md.us> wrote in message
news:3F183117...@dllr.state.md.us...

Dean Wells [MVP]

unread,
Jul 21, 2003, 12:40:00 PM7/21/03
to
Thanks for the clarification Mark.

For the sake of my own sanity, do you consider this the definitive list
of differences and, of equal importance, what was the goal behind these
differences (assuming it's not simply an oddity)?

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Mark Zbikowski (MSFT)" <Ma...@online.microsoft.com> wrote in message
news:ODYdDP6T...@TK2MSFTNGP12.phx.gbl...

Mark Zbikowski (MSFT)

unread,
Jul 21, 2003, 3:30:58 PM7/21/03
to
Well... I peeked at the sources. The differences stem from
a desire in OS/2 to "fix" COMMAND.COM; to wit, that every
call (with extensions enabled) would change the errorlevel
in a manner so you could detect if a command failed or not.

Now... without spending a ton of time with the sources, I don't
know if this REALLY addressed that issue or not.


--
Disclaimer: This posting is provided "AS IS" with no warranties, and confers
no rights.

"Dean Wells [MVP]" <dwe...@mask.msetechnology.com> wrote in message

news:%23tnr%23a6TD...@tk2msftngp13.phx.gbl...

Tim Hill

unread,
Jul 21, 2003, 5:49:13 PM7/21/03
to
Not sure about "fame", but yes .. that's me. :)

-Tim

"Phil Robyn" <zipp...@uclink.berkeley.edu> wrote in message
news:O6JEWkyT...@tk2msftngp13.phx.gbl...

Tim Hill

unread,
Jul 21, 2003, 6:01:39 PM7/21/03
to
This is very interesting. I'm going to dig down further. Can anyone think of
any use fro this feature?

-Tim

"Ritchie" <qiournvdlirh...@hotmail.com> wrote in message

news:bfg0cl$e82v3$1...@ID-156657.news.uni-berlin.de...

Phil Robyn

unread,
Jul 21, 2003, 6:12:58 PM7/21/03
to
Tim Hill wrote:

> Not sure about "fame", but yes .. that's me. :)
>
> -Tim

Well, then, I want to thank you so much for writing the book. I
never hesitate to recommend it wholeheartedly to others looking
for a good book about batch file scripting. Any plans for a new
edition? :-)

Frank

unread,
Jul 21, 2003, 8:37:14 PM7/21/03
to
Tim Hill <#IZpqO9T...@tk2msftngp13.phx.gbl>...

^ This is very interesting. I'm going to dig down further.
^ Can anyone think of any use fro this feature?

It seems that your news server may be a bit slow so I'll quote part of
another message in this thread:

Mark Zbikowski (MSFT) article <ODYdDP6T...@TK2MSFTNGP12.phx.gbl>...
^ The differences between .CMD and .BAT as far as
^ CMD.EXE is concerned are:
^
^ With extensions enabled, PATH/APPEND/PROMPT/SET/ASSOC
^ in .CMD files will set ERRORLEVEL regardless of error.
^ .BAT sets ERRORLEVEL only on errors.


Cross-platform (95 - NT) compatibility. A batch script written for Windows 95
will be "more likely" to behave as expected on Windows NT as well.

Frank

Tim Hill

unread,
Jul 21, 2003, 9:10:26 PM7/21/03
to
I'm investigating that possibility of an updated edition now (one of the
reasons I started "lurking" here again :) Would be interested in any
feedback on new topics/issues that need covering.

Thanks for the recommendations :)

-Tim

"Phil Robyn" <zipp...@uclink.berkeley.edu> wrote in message

news:eOCk6T9...@TK2MSFTNGP11.phx.gbl...

Gregory Mamayek

unread,
Jul 22, 2003, 11:28:55 AM7/22/03
to
Sorry for not replying earlier, been working on buggy WebTrends SW...groans

Both ran on WIN2K PC in DOS prompt stating what they were. No issue with either.

Ritchie wrote:

--

Gregory Mamayek

unread,
Jul 22, 2003, 11:32:05 AM7/22/03
to
This is the original title for post: "Where can one find documentation on
Windows command shell?" dated :"Date: 28 Apr 2003 08:59:32 -0700"

The first one didn't run, but the second one did (listed further down in
thread).

I'm no expert, still learning the ropes here...LOL

geye...@gmail.com

unread,
Mar 7, 2015, 1:13:43 PM3/7/15
to
On Friday, July 18, 2003 at 1:45:49 PM UTC-4, Gregory Mamayek wrote:
> Gang:\>
>
> How come some files fire using .cmd, while others use .bat?
> When would you use either one?
>
> Is it because .cmd is for execution, while .bat is used to provide a
> data source (ie. like web page .htm using SQL to call data source .bat)?
>
> --
> Cordially,
> Greg
> A+ BS MCSE Web Developer
I only know that when I renamed all .bats to .cmds the cmds are not recognized when executed from the shell of a dos app. (eg Valet file manager in this case). As I noticed no difference in execution decided to stay with bats to be safe.

d...@webmagic.co.uk

unread,
Dec 22, 2016, 7:58:26 AM12/22/16
to
> How come some files fire using .cmd, while others use .bat?
> When would you use either one?
>
> Is it because .cmd is for execution, while .bat is used to provide a
> data source (ie. like web page .htm using SQL to call data source .bat)?

Well it's 2016 and I'm running Win 7 desktop and have downloaded PDF Sam (from pdfsam.org). I don't have admin privileges and I shouldn't need them either. Now, if I run a command prompt (cmd) and execute the script, pdfsam.bat, to start PDF Sam it works. However, no sort of shortcut or other tweak can get it to run directly. However, if I rename the file pdfsam.cmd I can run it from a shortcut without starting a command prompt first.

rafaelmi...@gmail.com

unread,
Nov 22, 2018, 8:59:10 AM11/22/18
to
I'm very sorry for not answering, I did not stay connected and I did not receive your messages until now, so I can connect to see if I could see how to correct the error of deleting all my Google history. And I do not know how I can recover forgiveness if I bother you
Reply all
Reply to author
Forward
0 new messages