Redirecting Error Messages & Prompts


Some commands produce prompts or error messages which (1) Can be redirected, and (2) Follow the prompt or error message with the original argument. These outputs can be reused as batch files to allow processing of the original arguments. 

This is probably the most popular way of putting known words at the beginning of your data line. Maybe not the words you wanted, but at least you don't have to use DEBUG. The basic idea here is to pass your entire data line(s) as an argument to one of the normal DOS commands, then have the DOS command kick out a message which will always contain the same first word (so we can make a batch file with that name) and will have our original data line on the same line. Remember, what we want is an error message, so be sure your data doesn't have what the command wants! If there is even a remote chance that legitimate data might be present, you should poison your data lines by numbering them. Here's a summary:


DATE Accepts unlimited input lines. Allows input piping. Output for each line input. Terminates on first blank line. First word in all lines must not be a date. Typical use:
::Remove blank lines
DIR | FIND " " > TEMP.TXT
::Add blank line at end
ECHO.>> TEMP.TXT
::Run data thru DATE, search for desired line
DATE < TEMP.TXT | FIND "Volume in drive" > TEMP.BAT

Typical contents of TEMP.BAT:
Enter new date (mm-dd-yy): Volume in drive C is MS-DOS

If you use DATE, you must create an ENTER.BAT that references your data starting at %4. FC is often used ahead of DATE to number the input data because it will poison the data (insuring no date info), remove blank lines, add a blank line at the end (to terminate DATE), and make the results easier to search with FIND for the desired line(s).

Typical use of DATE with FC:
dir > temp.bat
fc /n temp.bat nul | date | find " 2: " > temp.bat

Typical contents of TEMP.BAT:
Enter new date (mm-dd-yy):   2:      Volume in drive C is MS-DOS
The use of DATE described above is the single most useful trick in batch programming.

TIME Accepts unlimited input lines. Allows input piping. Output for each line input. Terminates on first blank line. First word in all lines must not be a time. Typical use:
::Remove blank lines
DIR | FIND " " > TEMP.TXT
::Add blank line at end
ECHO.>> TEMP.TXT
::Run data thru TIME, search for desired line
TIME < TEMP.TXT | FIND "Volume in drive" > TEMP.BAT

Typical contents of TEMP.BAT:
Enter new time: Volume in drive C is MS-DOS

If you use TIME, you must create an ENTER.BAT that references you data starting at %3. FC cannot be used ahead of TIME because the 1:, 2:, etc. outputs are interpreted as legitimate times. FIND with the /N option may be used if necessary (yuk).

Typical use of TIME with FIND:
dir > temp.bat
type temp.bat | find /n /v "unlikely" > temp.bat
echo.>> temp.bat
type temp.bat | time | find "[2]" > temp.bat

Typical output of TEMP.BAT:
Enter new time: [2] Volume in drive C is MS-DOS
TIME is not used as often as DATE because TIME is not compatible with FC.


MOVE Accepts unlimited command-line parameters. No input piping. No arguments should be file, directory, or device names. The last argument will not be processed. If more than two arguments are supplied, the final argument must be a directory which exists (use a single period to indicate the current directory, which always exists). Outputs a separate line for each argument. Typical use:

MOVE one two three four . > TEMP.BAT
Contents of TEMP.BAT:
Cannot move one - No such file or directory
Cannot move two - No such file or directory
Cannot move three - No such file or directory
Cannot move four - No such file or directory

If you use MOVE, you must create a CANNOT.BAT that references your data at %2.
Because of the requirement to use an exising directory as the last argument, I can see no widespread use for the MOVE error messages in batch programmimg.


MODE Accepts command-line parameters only. No input piping. Outputs first argument. First word must not be a system device name. Useful for extracting the drive or root portion of a file name. Typical use:

MODE TEST.TXT > TEMP.BAT
Contents of TEMP.BAT:
Invalid parameter - TEST

MODE C:\TEMP\TEST.TXT > TEMP.BAT
Contents of TEMP.BAT:
Invalid parameter - C

If you use MODE, you must create an INVALID.BAT that references your data at %3. NOTE: unlike almost every other command, MODE under Windows 95 does not add a CR/LF at the end of it's output line! In other words, it generates what I refer to as a "line fragment" which can be concatenated with another line to create a new line:

echo test data> test.txt
mode = > temp.bat
copy temp.bat + test.txt
Results in a TEMP.BAT containing:
Invalid parameter - = test data
Use an equals sign as the argument because it, like spaces and commas, is a "delimiter". Your TEMP.BAT won't process the equals sign, so you can write an INVALID.BAT which can start processing data at %3.
Using the MODE error message is the quickest way to create a line fragment.

EXPAND Requires two non-blank lines, unlimited blank lines. Allows input piping. Outputs first non-blank line. First word in line must not be a file name. Typical use:
::Get desired line
DIR | FIND "Volume in drive" > TEMP.TXT
::Add dummy second line
ECHO FOO >> TEMP.TXT
::Run data thru EXPAND, search for desired line
EXPAND < TEMP.TXT | Find "Error -" > TEMP.BAT

Typical contents of TEMP.BAT:
Error - Can't open input file: Volume in drive C is MS-DOS

If you use EXPAND, you must create an ERROR.BAT that references your data starting at %6.
The best part about using the error message from EXPAND is that it ignores all blank lines in the input data file.


ATTRIB Accepts single command-line argument only. If more than one argument is supplied, an error message will go to the screen and a zero-byte file will result from any redirection. No input piping allowed. Argument must not be a file name. Typical use:

ATTRIB one > TEMP.BAT
Contents of TEMP.BAT
File not found - one

Here is an example of what happens if multiple arguments are supplied:

E:\>ATTRIB one two > TEMP.BAT
Parameter value not allowed - two  << this is displayed on-screen

E:\>dir TEMP.BAT

  Volume in drive E is HDC1_TEST
  Volume Serial Number is 224B-B508
  Directory of E:\

TEMP    BAT         0  04-19-97  5:04p  TEMP.BAT
        1 file(s)          0 bytes
        0 dir(s)   9,578,496 bytes free

If you use ATTRIB, you must create a FILE.BAT that references your data at %4.
The ATTRIB error message might be used to check that only one argument exists on a line of data, but the on-screen error display may (or may not) be a problem.


FC Accepts two command-line parameters only. No input piping. Neither argument should be a file or device name. Outputs first argument. Typical use:

FC one two > TEMP.BAT
Contents of TEMP.BAT:
File(s) not found : one

If you use FC, you must create a FILE(S).BAT that references your data at %4.
FC in this context is nearly useless and is included only for completeness.


Lost? Look at the site map.

Bad links? Questions? Send me mail.

Google
Yahoo
Ask Jeeves