DOS Batch in Plain English
Home News & Articles Tutorials Downloads About this site

Pipes, Redirection and Switches

A pipe is very handy for chaining commands together and sending information to different commands.  For example, if you type 'date' you can set the system date.  Once you type date, you can keep the current date by simply pressing enter.  If you remember echo+period gives you the equivalent of pressing enter.  You can type 'echo. |date' and it will just display the date.  You can do the same with 'echo. |time'.  Let's say that you wanted to find a particular line in something.  You can use:

echo. |date|find "Current"

Breakdown: echo. presses enter for the date program which would just sit there waiting for a response.  The date command completes after echo presses return and passes this whole bit of text onto the program "find":

Current date is Sun 01-30-2000

Enter new date (mm-dd-yy):

Find then searches for the text "Current" and spits back the line "Current date is Sun 01-30-2000".  This is very useful for advanced routines.  You can search for text in this way.  Check out 'find /?' for more options.

Redirection is simply moving data to different places.  You can pipe a directory list to a text file.  This is good for printing out file lists for you CDR covers and such.  Make this batch file in C:\Windows\Desktop:

@ECHO OFF

SET DOTS=%DOTS%.

ECHO %DOTS% > DOTS.TXT

The greater sign takes what would have been displayed and makes a file out of it.  Run this batch file from the command prompt and you will see a text file appear on your desktop.  If you are in the C:\Windows\Desktop directory in DOS, type 'type dots.txt'.  Type is a program that will display files.  You will see that it has one lonely period in it.  If you run it again from the command prompt, the one greater than sign tells Command.com to redirect and overwrite dots.txt with the contents of the variable DOTS.  If we want to add to dots.txt we can *append* (appending to a file means adding to the end of a file ).

Two greater than signs appends to the output file.  Let's change that line in our little batch file to say:

ECHO %DOTS% >> DOTS.TXT

And run the batch file again from the DOS prompt.  If you try to double click on the batch file on the windows desktop, our DOTS variable will be reset and a point of this exercise will be lost.  Run 'type dots.txt' again to see that dots.txt is two lines now.  Run the batch file again and you will see what I'm getting at.

Switches are a wonderful way of adding functionality to your script.  Switches can offer additional options and most importantly: help.  And a help routine is sometimes the best thing you can do for your batchy program.

@ECHO OFF

REM A batch file with help

if "%1"=="/?" goto help

echo This is the part of the program that runs.

goto end

:help

echo.

echo A batch file with help v1.0 -- created in Plain English

echo Usage:  yada yada

echo.

:end

This way you give your user a chance out there with your batch file.  It's good practice.  Not to mention you can add other options if you wanted to.

previous

next

Page 4

Page 1 - Introduction

Page 2 - The beginning

Page 3 - Variables, Loops and What-ifs

Page 4 - Pipes, Redirection and Switches

Page 5 - Where to go with batch

hide

A Pipe will take the information from one program and send it, or pipe it to the next.  The character is a '|'.  Not an l.  Not a 1.  It's a 'Shift+\'.  Right above the enter key on most keyboards.

hide

A Switch is a program option.  Most switches are invoked with a "/" and a letter.