DOS Batch Programming

General Rules

Named parameters are enclosed in percentage signs, as in %name% while input parameters are numeric and use only one percentage sign, as in %1.

WAIT

In command prompt there is no direct support for timed wait command. However, there is Choice command that has a time parameter that indicates how long it should wait for selection. This can be used for timing.

In Windows 7 this can be done in the following way (in this example we wait for 5 seconds):

CHOICE /N /C Y /T 5 /D Y >NUL

Calling another batch file

call anotherfile.bat

link

Directories

Current Directory

%CD%

Changing Directories

Use PUSHD/POPD to switch directories.

IF Command

Example of conditional branching:

@ECHO OFF
:TOP
   IF (%1)==() GOTO END
   ECHO Value is "%1" and still running...
   SHIFT
GOTO TOP
:END

See attached if.cmd.txt file for example.

Date and Time

See attached script.

Parameters

References:

Links

    • Batch files primer (link)
    • Windows XP Command Syntax (link).
    • Top 10 DOS Batch Tips (link)