tags:

views:

200

answers:

3

When you make a microsoft batch file, ie: saving

@echo off
:loop
echo HI!
goto loop

as hiloop.bat, how does the code work? Originally I thought that it worked just like any other code like C++, but then I read somewhere that a batch file directly works the OS, or something like that which made it extremely efficient, so

How does a microsoft .bat file work?

+2  A: 

It's entirely interpreted as slow as can be. A program (command-line processor) interprets each line one at a time, figures out what it wants to do, does it, and either checks the next line or goes where it's told to go.

However, since a batch file can invoke the assistance of COM dlls (which are essentially compiled programs), whatever is done within those can be very efficient. And there are thousands of dlls with chunks of functionality like database access, use interface components, and most everything you'd need to build many difficult but conventional things.

le dorfier
A: 

Batch files are executed in an interpreted manner by the shell (in Windows, this is cmd.exe). The shell reads each line of the file starting from the top, and executes the line as a command as if you had typed it in to the command prompt.

There are exceptions such as the goto command, which doesn't make sense if you type it at the command prompt, but does when executed in a batch file. When seeing the goto command, the shell will go back to the start of the file and read each line in turn until it finds the label indicated in the goto. Then it starts executing from there.

The above method is interpreted and not compiled, so it is much less efficient at execution than a compiled language like C++. I would never call batch files "extremely efficient". Maybe "extremely inefficient".

Greg Hewgill
+2  A: 

Batch files that end with .bat are interpreted by command.com and run under MS-DOS emulation.

Batch files that end with .cmd are interpreted by cmd.exe and run as Windows console applications.

You can start the command interpreter interactively by using Start, Run, and then typing either command.com or cmd.exe. It will prompt you for commands and will execute each line after you type it. A batch file basically just specifies the commands ahead of time so that you do not have to type them. The batch file is not preprocessed or compiled, so there is little or no efficiency gain.

There are some features that only work in batch files, such as the goto command which lets you create loops and branches to control the execution flow within the batch file.

binarycoder