Difference between .cmd & .bat files?

G

Gregory Mamayek

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)?
 
G

Gregory Mamayek

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?
 
D

Dean Wells [MVP]

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
 
G

Gregory Mamayek

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 said:
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 said:
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)?
 
D

Dean Wells [MVP]

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 said:
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 said:
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 said:
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)?
 
R

Ritchie

Dean Wells said:
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.
 
N

NuT CrAcKeR

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
 
D

Dean Wells [MVP]

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
 
D

Dean Wells [MVP]

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 said:
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 said:
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.
 
D

Dean Wells [MVP]

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
 
R

Ritchie

Dean Wells said:
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>
 
P

Phil Robyn

Tim said:
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)?
 
D

Dean Wells [MVP]

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.
 
D

Dean Wells [MVP]

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
 
M

Mark Zbikowski \(MSFT\)

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 said:
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
 
T

Tim Hill

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

-Tim
 
P

Phil Robyn

Tim said:
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? :)
 
F

Frank

Tim Hill <#[email protected]>...

^ 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 <[email protected]>...
^ 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
 
G

Gregory Mamayek

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top