home *** CD-ROM | disk | FTP | other *** search
- Get SET
- (PC Magazine Vol 6 No 1 Jan 13, 1987 User-to-User)
-
- You can access the SET environment strings in batch files simply
- by putting the set name inside a pair of percent signs (%). This lets
- batch files use the environment as a global memory area. For example,
- with a subdirectory on drive A: called |DOS, type:
-
- SET DOIT=A:\DOS
-
- Then create a batch file called D.BAT:
-
- DIR %DOIT%
-
- Typing D at the DOS prompt will display the directory listing for
- A:\DOS.
- Editor's Note: SET has been around for a long time as an
- undocumented command, and from the pathetic description in the DOS
- 3.x manual, it might just as well be undocumented. This technique
- does work but just scratches the surface. You can see the current
- state of your environment, including all the variables you've set,
- by typing SET.
-
- -----------------------------------------------------------------
- Easy Global Erasing
- (PC Magazine Vol 6 No 2 Jan 27, 1987 User-to-User)
-
- If you frequently delete all the files in the current directory
- with the command DEL *.* and are tired of answering the question, "Are
- you sure (Y/N)?", try the file DALL.BAT:
-
- ECHO OFF
- REN *.* $*.* >NUL
- DEL $*.* >NUL
- IF EXIST *.* DEL *.* >NUL
-
- This fools DOS into thinking it's deleting just a subset of all
- the files in the directory. It renames all the files to have a "$" as
- the first character, then deletes those files. The >NUL sends the "n
- files deleted" message into the Twilight Zone.
- In the rare cases that two filenames differ only in the first
- letter, or if the first letter is "$", some files will not get deleted.
- Therefore, after the first delete, we delete anything left over. In
- real life this almost never occurs, but if it does all that will happen
- is that you'll get the "Are you sure (Y/N)?" prompt.
- To enhance this so that you get no error messages if you run it
- on an empty directory, IF EXIST clauses may be added to the REN and
- DEL commands.
- Editor's Note: You could erase all the files in a directory with
- this ERASE.BAT file:
-
- ECHO OFF
- FOR %%F IN (*.*) DO DEL %%F >NUL
-
- If you omit a carriage return from the end of the second line,
- you'll see each file deleted one by one. However, if you include a
- carriage return, while you won't see the individual file deletion
- messages, you'll get a "Batch file missing" error message.
- The technique described above is clever, but it won't work
- properly since it will delete itself halfway through the process and
- grind to a halt. It's possible to adapt it, with a pair of batch
- files. First, DALL.BAT:
-
- ECHO OFF
- MD UPONE
- CD UPONE
- COPY ..\DELALL.BAT >NUL
- COMMAND /C DELALL
- CD ..
- DEL UPONE\DELALL.BAT
- RD UPONE
- DEL DALL.BAT
-
- Next, DELALL.BAT:
-
- COPY ..\DALL.BAT >NUL
- :START
- CTTY NUL
- REN ..\*.* $*.*
- CTTY CON
- DEL ..\$*.*
- IF EXIST ..\*.* GOTO START
- COPY DALL.BAT .. >NUL
- DEL DALL.BAT
-
- The idea intended here is to create another subdirectory that is
- located one level up and copy the batch file in and out, but that's
- extremely clunky. The one real improvement is that if the DELALL.BAT
- file does trip over two files that are exactly the same except for the
- first letter, it will simply start over again. DELALL.BAT assumes that
- you don't have any files that begin with $. If you did have such
- files, you could also add a third IF statement to rename them to
- something else and then delete those -- but the two batch files are
- overly complex already.
- Also, be sure to note that >NUL won't suppress error messages,
- but CTTY NUL will. If you use CTTY NUL be very careful to bring your
- system back with the command CTTY CON. And even this technique won't
- handle the missing batch file error you'll get if you insert a carriage
- return at the end of the last line of DELALL.BAT.
- The obvious solution to this problem is to create a one-line batch
- file that you use from one subdirectory to erase all the files in
- another subdirectory. If you're in \1\2\3 and you want to delete
- everything in \1\2\3\4, you can do it with the batch file containing
- the line:
-
- DEL \1\2\3\4*.* < Y
-
- For this solution to work you would need a tiny file on \1\2\3
- called Y that contained simply two characters -- a Y and a carriage
- return. If you wanted, you could try this with replaceable parameters,
- but you'd have to be careful that you didn't erase the wrong files
- through a silly but fatal typing error.
-
- -----------------------------------------------------------------
- Real Batch Variables
- (PC Magazine Vol 6 No 3 Feb 10, 1987 User-to-User)
-
- DOS can use replaceable parameters in batch files but has no way
- to turn replaceable parameters into true variables. It can be done --
- this one works with DOS 3.1 only!
- Use the DEBUG mini-assembler and redirect the INPUT.SCR file:
-
- DEBUG < INPUT.SCR
-
- THis will create a modified version of COMMAND.COM called ALTCOM.COM.
- ALTCOM.COM uses the space originally taken up by the VER and RMDIR
- commands and replaces these with a new INPUT command. (You'll still
- be able to use RD to remove subdirectories, but not RMDIR. And VER
- will be missing a space or two, as well as its trailing carriage
- return.)
-
- N COMMAND.COM
- L
- A 246E
- MOV SI,3C03
- INC CX
- SUB [SI],CL
- MOV DX,SI
- MOV AX,0C0A
- INT 21
- INC SI
- DEC DI
- LODSB
- MOV CL,AL
- INC CX
- REPZ MOVSB
- CALL 29DG
- JMP 270F
-
- E 4D9B "INPUT",02,2E,11
- E 4CEB 49
- E 4D6D 49
- E 4A4E "3.1",00,00,00,00,00,00
- N ALTCOM.COM
- W
- Q
-
- The syntax for INPUT is:
-
- INPUT variable
-
- When DOS sees the INPUT command in a batch file, it will wait for input
- from the keyboard. This information will then be placed in the DOS
- environment for the batch file to use.
- To see this in action, create the following TEST.BAT batch file.
- (Remember, this works in DOS 3.1 only.)
-
- ECHO OFF
- CLS
- :LOOP
- ECHO COMMAND?
- INPUT CMD=
- IF %CMD%! == ! GOTO LOOP
- IF %CMD% == STOP GOTO END
- IF %CMD% == stop GOTO END
- %CMD%
- GOTO LOOP
- :END
-
- Before you try it, however, install your new command processor by
- typing in ALTCOM at the DOS prompt. Then run the batch file. To get
- out of the batch file just type STOP (or, stop).
- The number of variables you can use is limited only by the amount
- of environment space you have. If you like this, you can rename
- ALTCOM.COM to COMMAND.COM and use it to replace your old command
- processor.
- If you type:
-
- SET <parameter1>=<parameter2>
-
- and the name of parameter1 is longer than the environment space, DOS
- responds with an "Out of environment space" error. This is fine but
- when you type SET to find out what you have in the environment, you
- find the environment contains part of the name of parameter1. Also
- you can no longer set anything else into the environment unless you
- erase something previously put in. Besides, there is no way of
- removing that string short of restarting the computer.
- Editor's Note: This patch is interesting, even though it mangles
- some existing commands. DOS batch files are powerful tools, but users
- sorely miss this kind of interaction. The sample TEST.BAT file above
- lets you execute interactive DOS commands on the fly or pass parameters
- all around your system.
- If you try this, remember you have to load ALTCOM.COM as a
- secondary command processor to make it work (type EXIT to return to
- yoru original COMMAND.COM when you're done).
- Finally, in DOS 3.2, you can set environment size, using a
- /E COMMAND.COM switch. And if you're running short of space in your
- environment, you can always use the SUBST command to shorten your
- paths, which are the big space hogs.
-
- -----------------------------------------------------------------
- Well-Behaved Batch Files
- (COMPUTE! Magazine February 1987 by Ulf Larsson-Westlund)
-
- YORN.COM helps make batch files more interactive. When invoked
- from DOS, it displays a yes/no prompt and waits for you to press an
- indicated key, returning an error code which the batch file can use
- to branch to different parts of the command process.
- To see what YORN.COM does, type YORN at the DOS prompt and press
- Enter. The computer displays the prompt: Answer (Y)es or (N)o ...
- and waits for you to indicate your choice. YORN.COM recognizes only
- the characters Y, y, N, or n.
- An example will show how YORN.COM works in this YESNO.BAT file:
-
- ECHO OFF
- CLS
- :START
- ECHO THIS IS A TEST BATCH FILE FOR YORN.COM
- YORN PLEAS PRESS N TO CONTINUE ...
- IF ERRORLEVEL 255 GOTO WRONG
- ECHO
- ECHO YOU PRESS THE N KEY
- GOTO END
- :WRONG
- ECHO
- ECHO YOU DIDN'T PRESS THE N KEY
- GOTO START
- :END
- ECHO
- ECHO ...ENDING
-
- Type YESNO at the DOS prompt. Note that YORN.COM displays a
- different prompt this time. Instead of Answer (Y)es or (N)o, it
- prints the message PLEASE PRESS N TO CONTINUE. If you answer "Yes"
- by pressing Y or y, one series of batch commands is executed. If you
- answer "No~ by pressing N or n, the batch file branches to a different
- series of commands.
- It's not difficult to see how this capability might be useful.
- For instance, say you often boot up with an AUTOEXEC.BAT file that
- installs an accessory program such as SideKick. When you use memory-
- intensive software such as Framework on a machine with only a limited
- amount or RAM, you may find yourself running out of memory if SideKick
- or a similar accessory is resident. With YORN.COM, your AUTOEXEC.BAT
- file can ask you whether or not to install the accessory and respond
- accordingly.
- YORN.COM tells you which key is pressed by returning an error
- code. In YESNO.BAT, it returns an error code of 255 when you press Y
- or y and an error code of 254 for N or n. You can check the error
- code with IF-ERRORLEVEL and branch to the desired destination with
- GOTO as shown in the sixth line of YESNO.BAT. When you're checking
- error codes, it is essential to begin with the highest code (255 in
- this case) and work downward to lower codes systematically.
- To change the prompt printed by YORN.COM, simply supply the text
- of the new prompt after the word YORN in the batch file. If no such
- text is found, YORN.COM prints the default prompt.
- For special purposes, you can also check for characters other
- than Y or N. For instance, a batch process that can send output to
- either the screen or a disk file might prompt you to press S for
- screen output or D for disk output.
- The hex numbers $59 and $79 in lines 390 and 400 of the BASIC
- program stand for the characters Y and y, respectively. The hex
- numbers $4E and $6E in lines 400 and 410 stand for N and n,
- respectively. To substitute other characters, replace these values
- with the values of the characters you wish to test for. Remember that
- these numbers must be in hex. (The BASIC function HEX$ converts
- decimal values to hex; for instance, PRINT HEX$(13) displays 0D, the
- hex equivalent of decimal 13.) If you change any of these values, you
- must also change the checksum value (10731) in line 170 accordingly.
- Once this is done, rerun the BASIC program to create a new version of
- YORN.COM.
-
- 100 'YORN.BAS: Creates YORN.COM
- 120 PRINT "Checking DATA ....";
- 130 FOR I=0 TO 109
- 140 READ A$:A=VAL("&H"+A$)
- 150 CKSUM=CKSUM+A
- 160 NEXT I
- 170 IF CKSUM=10731 THEN 210
- 180 PRINT:PRINT
- 190 PRINT "Error; check your typing."
- 200 STOP
- 210 RESTORE 340
- 220 OPEN "YORN.COM" AS #1 LEN=1
- 230 FIELD #1,1 AS BYTE$
- 240 FOR I=0 TO 109
- 250 READ A$
- 260 LSET BYTE$=CHR$(VAL("&H"+A$))
- 270 PUT #1
- 280 NEXT I
- 290 CLOSE #1
- 300 PRINT:PRINT
- 310 PRINT "YORN.COM created."
- 320 PRINT
- 330 END
- 340 DATA EB,05,0D,20,20,1A,08,BE,80,00
- 350 DATA B5,00,8A,0C,83,F9,00,75,0A,BA
- 360 DATA 54,01,B4,09,CD,21,EB,19,90,46
- 370 DATA 8A,5C,01,80,FB,0D,74,08,8A,D3
- 380 DATA B4,02,CD,21,E2,EF,BA,6D,01,B4
- 390 DATA 09,CD,15,B4,00,CD,16,3C,59,74
- 400 DATA 11,3C,79,74,0D,3C,4E,74,04,3C
- 410 DATA 6E,75,EC,B0,FE,EB,03,90,B0,FF
- 420 DATA B4,4C,CD,21,41,6E,73,77,65,72
- 430 DATA 20,59,28,65,73,29,20,6F,72,20
- 440 DATA 4E,28,6F,29,20,2E,2E,2E,24,24
-
- -----------------------------------------------------------------
- Batch File Bonanza
- (PC World February 1987 Star-Dot-Star)
-
- DOS lacks an input command that can accept and pass a user's
- response to other DOS commands. If this capability were available,
- a batch file could ask the operator if a particular command should
- exercise a given option during execution.
- For example, the DIR command can be issued with the /P or /W
- option. With an input command, a batch file could ask if the directory
- listing should pause when the screen fills or be displayed in wide
- format. Once the batch file received a response, the DIR command
- would execute.
- A DOS input command would also be handy in a batch-file-driven
- menu system. A batch file could display a menu and rely on the input
- command to accept and execute the user's choice.
- Until Microsoft includes such a command in its next version of
- DOS, you can achieve the same result -- capturing user input -- with
- the COPY command. MENU.BAT is an example of how the technique could
- be applied to a menu system. MENU.BAT begins by changing the DOS
- prompt to:
-
- Enter A:MENU to display menu A>
-
- The program then tests for a parameter and checks whether it's
- valid or not. When you call the batch file without a parameter (by
- simply typing: MENU <Enter>), the first IF test branches execution
- to the label :getinput. The following lines save the current DOS
- prompt in a DOS variable called TEMP; use PROMPT to command ANSI.SYS
- to redefine the <Enter> key as Ctrl-Z <Enter> and remove the DOS
- prompt; clear the screen; and use ECHO commands to display the menu.
- (Note that the ANSI.SYS command uses a lowercase p.) (You must install
- the ANSI.SYS driver in your system's CONFIG.SYS file before the PROMPT
- command can redefine the keyboard.)
- COPY CON:RESPONSE.DAT creates a file called RESPONSE.DAT containing
- the user's response entered from the keyboard. (Ctrl-Z <Enter> must be
- used to inform the COPY command that the response is complete -- hence
- the redefined <Enter> key.) >NUL suppresses the message '1 file(s)
- copied' by redirecting it to the nul (nonexistent) device.
- The next COPY command concatenates COMMAND.DAT and RESPONSE.DAT,
- creating the file CONTINUE.BAT. COMMAND.DAT simply contains the word
- MENU followed by a space. Before you run MENU.BAT, create COMMAND.DAT
- in the default drive by typing:
-
- COPY CON:COMMAND.DAT <Enter> MENU
-
- and a single space. Finish up by typing Ctrl-Z and <Enter>.
- With CONTINUE.BAT in place, MENU.BAT uses PROMPT to restore the
- <Enter> key's original value, then removes the variable TEMP from the
- DOS environment, clears the screen, and calls the file CONTINUE.BAT,
- which it just created.
- CONTINUE.BAT issues MENU, along with the user response stored in
- RESPONSE.DAT. MENU.BAT runs anew, and because a parameter is supplied
- this time, the first IF test is negative and execution falls to the
- next IF test. When a match is finally found, execution branches to
- the appropriate label and the desired menu item is executed. If there
- is no match, MENU.BAT displays the message 'Invalid response'. Press
- a key and the menu reappears, ready for input.
-
- echo off
- prompt Enter A:MENU to display menu $_$n$g
- cls
- if %1!==! goto getinput
- if %1!==1! goto 1
- if %1!==2! goto 2
- if %1!==3! goto 3
- echo Invalid response
- pause
- :getinput
- set temp=%prompt%
- prompt $e[13;13;26;13p
- echo on
- echo off
- cls
- echo Enter your choice:
- echo -
- echo 1 for Microsoft Word
- echo 2 for Lotus 123
- echo 3 for dBASE III
- copy con:response.dat >nul
- copy command.dat+response.dat continue.bat >nul
- prompt $e[13;13p
- echo on
- echo off
- prompt %temp%
- set temp=
- cls
- continue
- :1
- word
- :2
- 123
- :3
- dbase
-
- -----------------------------------------------------------------
- Batch Subroutines
- (PC World February 1987 Star-Dot-Star)
-
- DOS does not support batch file subroutines -- in other words,
- when one batch file calls another, DOS cannot return control to the
- first batch file after the second one ends.
- However, you can overcome this limitation by loading a second
- copy of COMMAND.COM, the DOS command processor, to execute the second
- batch file. When the second batch file ends, the secondary command
- processor returns control to the first. Execution resumes immediately
- after the line in the first batch file that invoked the second
- COMMAND.COM.
- This technique is illustrated in CALL.BAT:
-
- echo off
- echo This is the first batch file
- echo Ready to execute the first subroutine
- command/c sub1.bat
- echo Now back to the main batch file
- echo Transferring control to the second subroutine
- command/c sub2.bat
- echo Now back to the main batch file again
-
- The first few lines display a message, then COMMAND/C SUB1.BAT executes
- the batch file SUB1.BAT:
-
- echo This is the FIRST subroutine batch file
- pause
-
- The /C parameter forces DOS to terminate COMMAND.COM when the named
- batch file ends. (If you omit the /C switch, the last line of SUB1.BAT
- must be EXIT so that execution can be returned to the primary command
- processor; otherwise, commands continue to be processed by the
- secondary command processor.) When SUB1.BAT ends, execution continues
- at the fifth line of CALLBAT.BAT, more messages are displayed and
- SUB2.BAT:
-
- echo This is the SECOND subroutine batch file
- pause
-
- is executed in the same manner. When SUB2.BAT finishes, control
- returns to CALLBAT.BAT, which then ends.
- This technique can be extended -- a second batch file calling a
- third, a third a fourth, and so on -- as long as memory is available
- and a copy of COMMAND.COM is loaded for each batch file. Because every
- copy of COMMAND.COM creates a new environment, information such as ECHO
- status or DOS errorlevel value cannot be passed between batch files.
- By using DOS redirection, a second copy of COMMAND.COM (and any
- program run by it) can receive input from a disk file instead of from
- the keyboard. This feature makes it possible to automate virtually
- any operation using batch files.
-
- -----------------------------------------------------------------
- Fast File Scanner
- (PC Magazine Vol 6 No 6 Mar 31, 1987 User-to-User)
-
- A simple way to display lots of batch files is to create two batch
- files called SCANBATS.BAT and READ.BAT. First, SCANBATS.BAT scans
- through all the .BAT files on a disk:
-
- echo off
- for %%f in (*.bat) do command /c read %%f
-
- Then, READ.BAT, which is called by SCANBATS.BAT, performs the actual
- displaying:
-
- echo off
- cls
- echo %1
- type %1 | more
- pause
-
- To try it, type in both files using a pure ASCII word processor or the
- DOS COPY CON command, and then type SCANBATS.
-