Wednesday, April 4, 2018

windows - What is the difference from using bat and cmd?



I have tried Google, but perhaps Google mojo is bad, since I have not found and suitable explanation



I have a batch file that stops and starts some services.
This batch file is then scheduled in task scheduler using a service account.



If I change the extension from .bat to .cmd then the script fails, since the user does not appear to have the rights to stop and start a service.




But why does this change behavior just by changing the extension of the script?


Answer



What is the difference from using bat and cmd?




BAT was created to interact with COMMAND.COM, the command interpreter
of DOS. Microsoft adopted most of the DOS commands into their new
interpreter named CMD. EXE. CMD was created to interface with CMD.EXE
and it breaks compatibility with COMMAND.COM. Another key difference

is in how they handle the errorlevel variable. When using BAT, this
variable is only changed once an actual error occurs and no change in
state occurs when the each command executes successfully. This is not
true for CMD as the errorlevel variable would still change state even
if no errors occur. Programmers should take note of this when creating
elaborate scripts as it may cause a little bit of confusion.



Aside from those minor differences, CMD and BAT are identical to each
other. Most users who create simple scripts to clear or transfer files
around should not encounter any problem. For users of the more recent

versions of Windows, BAT and CMD are pretty much interchangeable as
CMD.EXE would interpret and execute the commands in both files.
Although most users are aware of this fact, a lot of the older people
who had a chance to work with DOS and its batch files still use the
BAT extension; simply out of habit and familiarity.




Summary:



1. The BAT extension is used by DOS and Windows while the CMD extension is for Windows NT Command Scripts




2. The BAT extension can be interpreted by COMMAND.COM and CMD.EXE while the CMD extension can only be interpreted by CMD.EXE



3. The errorlevel always changes state in CMD but only on errors in BAT




source








If I change the extension from .bat to .cmd then the script fails



In order to fully give you an accurate answer on this portion of your question, the logic you are using needs to be disclosed in order to understand what it's doing to help troubleshoot.



Otherwise, I would assume the failure you are seeing has to do with how the .CMD scripts handle errorlevel differences or perhaps some Group Policy or other restrictions that affects the interpretation by CMD.exe and not when run and interpreted by COMMAND.COM.






Further Resources





No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...