Batch file macros

Other topics

Basic Macro

Using DOSKEY, we can create macros to simplify typing many commands in command prompt. Take a look at the following example.

DOSKEY macro=echo Hello World

Now if you type macro in the command prompt, it would return Hello World.

Comments

Unfortunately, DOSKEY macro doesn't support comment, but there's a workaround.

;= Comment
;= Comment
;= Remember to end your comment with ;=
;=

$ Character Usages

There are 3 usages of the $ character in a DOSKEY macro.


Command separator

$T is the equivalent of & in a batch script. One can join commands together like so.

DOSKEY test=echo hello $T echo world

Command-line arguments

Like bash(not batch), we use $ to indicate command-line argument.

$1 refers to the first command-line argument

$2 refers to second command-line argument, etc..

$* refers to all command-line argument

Macros In Batch Script

DOSKEY macros don't work in a batch script. However, we can use a little workaround.

set DOSKEYMacro=echo Hello World
%DOSKEYMacro%

This script can simulate the macro function. One can also use ampersands(&) to join commands, like $T in DOSKEY.

If you want a relatively large "macro", you may try a simple function or take a look at other function topics here.

Contributors

Topic Id: 10791

Example Ids: 32356,32357,32358,32382

This site is not affiliated with any of the contributors.