batch-file Getting started with batch-file

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

From Microsoft Technet:

With batch files, which are also called batch programs or scripts, you can simplify routine or repetitive tasks. A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the filename at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.

Batch File Names and Extensions

ExtensionRemarks
.batThis extension runs with MS-DOS and all versions of Windows
.cmdUsed for batch files in Windows NT family
.btmThe extension used by 4DOS and 4NT

To understand the difference between .cmd and .bat please see here.

Avoid names which are already the name of built-in commands. like tracert. There is a utility called tracert.exe. So, avoid naming a batch file tracert.bat

Running Batch File

The easiest way to run a batch file is simply double-clicking its icon. Or paste the file full path into a command prompt, or just its name if command Prompt was started from the batch file directory, then enter.

Example:

C:\Foo\Bar>test.bat
C:\Foo\Bar>C:\Foo\Bar\Baz\test.bat

Editing and Viewing Batch Files

Any ASCII editor can edit batch files. A list of editors that can syntax highlight batch syntax can be found here. You can also use the default notepad shipped with windows to edit and view a batch file, although it does not offer syntax highlighting.

To open notepad:

  • Press Win 𐌎+R, type notepad and then press Enter.

Alternatively, the most "primitive" way to create a batch file is to redirect output from the command line to a file, eg.

echo echo hello world > first.bat
 

which writes echo hello world to the file first.bat .

You can edit a batch file by right clicking the file and selecting "Edit" from the context menu.

To view the contents of a batch file from within a command prompt, run the following command:

type first.bat
 

You can also start editing your batch file with notepad from the command prompt by typing

notepad first.bat
 

Getting Help

To get help on a batch file command you can use the built-in help.

Open a command prompt (whose executable is cmd.exe ) and enter help to see all available commands.

To get help for any of these commands, type help followed by the name of the command.

For example:

help help
 

Will display:

Provides help information for Windows commands.

HELP [command]

    command - displays help information on that command.
 

Some commands will also display help if followed by /? .

Try:

help /?
 

Note:

Help will only display the help for internal commands.

Opening a Command Prompt

The command prompt comes pre-installed on all Windows NT, Windows CE, OS/2 and eComStation operating systems, and exists as cmd.exe , typically located in C:\Windows\system32\cmd.exe

On Windows 7 the fastest ways to open the command prompt are:

  • Press Win 𐌎, type "cmd" and then press Enter.

  • Press Win 𐌎+R, type "cmd" then then press Enter.

  • If you have an explorer window open, type "cmd" in the address bar to open a prompt in the currently selected directory.

  • Right-click a folder in Explorer while holding Shift and select "Open command window here".

It can also be opened by navigating to the executable and double-clicking on it.

In some cases you might need to run cmd with elevated permissions, in this case right click and select "Run as administrator". This can also be achieved by pressing Control+Shift+Enter instead of Enter when using way 1 of the points above.



Got any batch-file Question?