Difference .BAT / .CMD?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Difference .BAT / .CMD?

#1 Post by Eureka! » 02 Feb 2022 07:20

IIRC, there were some subtle differences between running a .CMD vs. running a .BAT script.

I can't remember what those were at the moment and my forum-search skills are apparently lacking.

Can someone help me get this straight?


EDIT:
To answer my own question (thanks, Google):
Stackoverflow wrote: 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.
(link)

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: Difference .BAT / .CMD?

#2 Post by sst » 02 Feb 2022 07:55

Eureka! wrote:
02 Feb 2022 07:20
Stackoverflow wrote: 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.
(link)
And Here is the link to the original statement by Mark Zbikowski

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Difference .BAT / .CMD?

#3 Post by Aacini » 02 Feb 2022 14:04

You may review the commands described under Table 3 at this SO answer...

Antonio

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Difference .BAT / .CMD?

#4 Post by Eureka! » 04 Feb 2022 05:33

Aacini wrote:
02 Feb 2022 14:04
You may review the commands described under Table 3 at this SO answer...
I did and .. thank you!


(and thanks for that quick date %date% admin-check too)

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: Difference .BAT / .CMD?

#5 Post by sst » 06 Feb 2022 03:07

Eureka! wrote:
04 Feb 2022 05:33
(and thanks for that quick date %date% admin-check too)
I don't want start a discussion about date %date%, especially here in this topic since it's about something else. But I'm against promoting bad and potentially dangerous practices, no matter how they seems to be clever or convenient to use.
Using date %date% to check for admin privileges is not only potentially dangerous which can even lead to disasters, depending on the environment and the use case, it also won't be a 100% reliable method and can lead to false positive. Here is why:
- It is dangerous because using date %date% to check for admin privileges is a form of Time-of-check to time-of-use. In simple words there is a slight time difference between reading the %date% value and executing the resulting command. This time deference can manifest itself in the middle of the night at 00:00. The %date% value is read at 2022-06-02 23:59:59.99 and it will be executed at 2022-06-03 00:00:00.02. It is not hard to see what happens if the script/command happens to be executed under admin privileges. Now Imagine if that happens on a production server; And they trusted your script...
- It's not reliable because a non-admin user account can be given the SeSystemtimePrivilege privilege by group policies. So while date %date% wont cause any error, still the user is not admin. hence the false positive.

Post Reply