home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
t
/
tsbat.zip
/
TSBAT.EXE
/
BATRICKS.TXT
< prev
next >
Wrap
Text File
|
1992-10-22
|
14KB
|
387 lines
Assorted Batch Tricks Thu 22-October-1992
=====================
By prof. Timo Salmi
Moderating at garbo.uwasa.fi anonymous FTP archives 128.214.87.1
Faculty of Accounting & Industrial Management; University of Vaasa
Internet: ts@uwasa.fi Bitnet: salmi@finfun ; SF-65101, Finland
..................................................................
┌────────────────────────────────────────────────────────────┐
│ This file belongs to TSBAT*.ZIP. Please do not distribute │
│ this batricks.txt file separately. │
└────────────────────────────────────────────────────────────┘
Introduction
============
This file contains assorted batch tricks. Many, but not all, have
been used in the TSBAT*.ZIP collection of batches. Likewise, there
are some useful further tricks, not documented here, to be found in
the TSBAT batches.
INDEX
=====
1) Making "@echo off" general
2) Deleting All Files
3) Nested Loops
4) Checking whether a directory exists
5) Checking that a program is available at the current directory or at path
6) Using subroutines in batches
7) Convert a parameter to uppercase
8) Appending a new directory to the path
9) Comparing two files
10) Writing an empty line
11) Customizing the pause message
12) Complicate renaming with for
13) Checking for wildcards
14) Preventing breaking the batch
15) Prevent a break from bypassing your autoexec.bat
16) Getting the extension
17) The quote character %
18) Eliminating auxiliary batches
19) Utilizing the subst command in paths
20) How to run a batch once a week (testing for the weekday)
1. Making "@echo off" general
=============================
If you want to turn the echo off, and do not wish to show that line
on the screen, you can easily do this by applying
@echo off
There is a catch, however, because this only works since MsDos
version 3.30. So if you want to make it general, put the following
line in your autoexec.bat file if you are using MsDos 3.30 or higher
set _echo=@
Then use the following format in your batches, which will then work
for any MsDos version
%_echo%echo off
2. Deleting All Files
=====================
One of the most Frequently Asked Questions (FAQs) about batches is
how to suppress the "Are you sure (Y/N)?" confirmation requirement
for del *.*. Use the following:
echo y| del *.*
If you wish to suppress the message too, use
echo y| del *.* > nul
Whether or not it is sensible to suppress the confirmation can be
debated, but this is the trick anyway.
3. Nested Loops
===============
It is possible to have nested loops of a kind in batch programming.
Consider the following two batches, and try it out by calling
test.bat.
echo off
rem TEST.BAT
for %%f in (a b c d e f) do %comspec% /c test2 %%f
echo off
rem TEST2.BAT
for %%g in (1 2 3) do echo %1%%g
4. Checking whether a directory exists
======================================
It is sometimes useful to be able to test whether a particular
directory exists. The following test is true if the %1 directory
does not exist.
if not exist %1\nul if not exist %1nul echo Directory %1 does not exist
5. Checking that a program is available at the current directory or at path
===========================================================================
When you call a program from a batch, and do not give the explicit
path to it, it is advisable to test that the program is available
either at the current directory or the default path.
set _found=
if exist %1 set _found=yes
for %%d in (%path%) do if exist %%d\%1 set _found=yes
for %%d in (%path%) do if exist %%d%1 set _found=yes
if "%_found%"=="yes" goto _continue
echo %1 is not at path or the current directory
goto _out
:_continue
echo %1 found at path or in the current directory
:_out
6. Using subroutines and recursion in batches
=============================================
It is possible to use subroutines within batches. The crucial trick
is setting an environment variable (eg _return) to point to a label
where to return after the subroutine has been performed. For an
example see UNPACK.BAT, and BOOT.BAT, the sections :_common and
:_subru.
7. Convert a parameter to uppercase
===================================
This example shows how to ensure that the parameter %1 given to the
batch is in uppercase. This utilizes the fact that MsDos converts
the path to uppercase. The result is stored in upcase_ and then the
original path is restored.
set tmp_=%path%
path=%1
set upcase_=%path%
path=%tmp_%
8. Appending a new directory to the path
========================================
This often needed trick is basically very simple. For example
to add directory %1 to path use
path=%path%;%1
Note that you can only use this trick in a batch. It will not work
at the MsDos prompt because the environment variables are expanded
(%path%) only within batches.
For a full treatment with safeguards against appending non-existing
directories, or appending twice, see ADDPATH.BAT.
9. Comparing two files
======================
It is possible in batch programming to test whether or not two files
have identical contents. This trick utilizes th external MsDos
programs fc.exe and find.exe. (An external MsDos program means, of
course, a program that comes with the standard MsDos releases. Most
often the MsDos external support files are located in a c:\dos
directory.)
fc %1 %2 > tmp$$$
type tmp$$$ | find /i "fc: no differences encountered" > diffe$$$
if exist notsame$ del notsame$$$
copy diffe$$$ notsame$ > nul
if not exist notsame$ echo Files %1 and %2 are different
if exist notsame$ echo Files %1 and %2 are identical
if exist tmp$$$ del tmp$$$
if exist notsame$ del notsame$
if exist diffe$$$ del diffe$$$
If you think about, this idea can be used for other useful purposes,
too, because it establishes whether a given string is found in a
text file.
10. Writing an empty line
=========================
This is a simple, but an often needed, useful trick. Just use echo
with (for example) a point (.) after it. As you can see, I have
utilized this batch feature extensively in my batch collection.
echo.
11. Customizing the pause message
=================================
You can easily customize the message given by pause by giving your
own with echo and directing the pause message to nul.
to nul.
echo Break to quit, any other key to remove the tmp directory
pause > nul
12. Complicate renaming with for
================================
Although this is basically trivial, one does not necessarily come to
thing of it. The for statement is quite useful for involved renaming
of files. An example delineates. For example I have the following
files (Turbo Pascal units for TP 4.0, 5.0, 5.5 and 6.0). Say that I
wish to rename them to be version 30 instead of 29.
tspa2940.zip
tspa2950.zip
tspa2955.zip
tspa2960.zip
The following for-statement does that conveniently.
for %f in (40 50 55 60) do ren tspa29%f.zip tspa30%f.zip
Naturally, renaming is not the only task that can utilize this
trick. I am sure you can readily think of others, like
for %d in (a b) do format %d:
13. Checking for wildcards
==========================
This example shows how you can test whether a parameter (%1) of a
batch contains wildcards.
@echo off
for %%f in (%1) do if "%%f"=="%1" goto _nowilds
echo Parameter %1 contains wildcards (or is missing)
:_nowilds
14. Preventing breaking the batch
=================================
It is possible to prevent user interrupt of a batch by using the
ctty command to reassign the input (and the output) device. Here is
an example (an elementary password batch requiring inputting an e).
Note the < and > redirections which are needed while the ctty has
been assigned to nul. The ask batch enhancer is included in the
TSBAT collection.
@echo off
ctty nul
echo Now you cannot break the batch with ^C or ^Break > con
:_ask
echo Use e to break > con
ask /b /d < con
if errorlevel==101 if not errorlevel==102 goto _out
goto _ask
:_out
ctty con
echo Back to normal. Now you can break the batch with ^C or ^Break.
Note that this trick does not prevent you from rebooting with
alt-crtl-del while the batch is running. For that you need an
external program like noboot.exe from garbo.uwasa.fi:/pc/ts/
tstsr13.zip (os whichever version number is current).
15. Prevent a break from bypassing your autoexec.bat
====================================================
You can actually prevent a quick tapping of the break from bypassing
your autoexec.bat by a variation of the trick in the item above. Put
for example
shell=c:\command.com /p nul
in your config.sys. Before you do, make sure to have a floppy to
boot from in case something goes wrong. I first saw trick when it
was posted in the UseNet comp.os.msdos.programmer newsgroup by
Joseph Gil yogi@cs.ubc.ca.
This is not, however, quite all there is to it. You should put
ctty con
as the last line to your autoexec.bat. If you don't, the keyboard
will not be responding, and you must boot from the floppy you so
sensibly had prepared :-).
16. Getting the extension
=========================
It would be quite useful to be able to extract the extension of a
given file name into an environment variable. Or to be able just to
test whether there is an extension. Here is how to do that. The
batch is based on the information in PC-Magazine July 1992, Vol 11,
No. 13, page 528. It gives the crucial information that if one
precedes the argument of a for loop with a slash (/), then the
argument is interpreted in two parts. The first part is the first
character of the argument, the second part all the rest. Neat,
indeed.
The problem with my solution below is that it will not recognize
.* or .??? as extensions. But, of course, one can first test for
wildcards as shown in a previous item "Checking for wildcards". See
e.g. UNPACK.BAT for the utilization of this method.
@echo off
set exten_=%1
:_next
set prev_=%exten_%
for %%f in (/%exten_%) do set exten_=%%f
if ".%exten_%"=="%prev_%" goto _extfound
if not "%exten_%"=="%prev_%" goto _next
goto _noext
:_extfound
echo The filename %1 has an extension %exten_%
goto _out
:_noext
echo The filename %1 has no extension
:_out
set exten_=
set prev_=
17. The quote character %
=========================
As we know %1 indicates the first parameter given to a batch. Thus
for example echo %1 echoes that parameter. But what if you ant to
echo the actual string %1 instead. The % character acts as a quote
character. Thus echo %%1 will indeed be a "%1" instead of its usual
interpretation. Try the following simple test
@echo off
if "%1"=="" goto _out
echo %1
echo %%1
:_out
See the item on "Eliminating auxiliary batches" for utilizing this
feature. A good example of utilizing this feature is given by
DELPATH.BAT.
18. Eliminating auxiliary batches
=================================
Quite a number of batch programming tasks require an auxiliary batch
which the primary batch has to call. Many of these cases can be
eliminated by making the batch call itself (a kind of recursion).
The auxiliary code is put in the batch itself. The trick is best
illustrated by looking at the SHOW.BAT, which provides a wild-carded
TYPE command, and would normally need an auxiliary file to type each
of the individual files. Another example is given by the SAFEDEL.BAT
batch.
There is also an another trick for a similar purpose. The primary
batch creates and auxiliary batch or batches, which it then calls.
See DELPATH.BAT for an example of this method.
There was an inventive twist of this method in PC-Magazine August
1992, Vol. 11, No. 14, p. 527 for getting the volume label of a
disk. Here is my own example using the same techniques. It sets the
current directory in an environment variable getdir_. I have
utilized this technique in PUSHDIRE.BAT.
@echo off
echo @echo off> director.bat
echo set getdir_=%%2>> director.bat
echo echo %%getdir_%%>> director.bat
dir | find "Directory"> go.bat
call go
if exist director.bat del director.bat
if exist go.bat del go.bat
19. Utilizing the subst command in paths
========================================
I use the following kind of a simple batch to make some of my
directories easy to reach. The way this simple batch is written it
avoids unnecessary errors if the substitution already has been made.
As a last measure it shows the current substitution status.
@echo off
if exist m:\nul echo The substitution has already been made
if not exist m:\nul subst m: c:\math
if not exist s:\nul subst s: c:\support
subst
20. How to run a batch once a week (testing for the weekday)
============================================================
The crucial trick is to be able to put the weekday into an
environment variable. For the full treatment see WEEKLY.BAT. The
essential trick needed is below, that is capturing the weekday into
a weekday_ environment variable. No auxiliary programs outside the
normal MsDos commands are needed.
@echo off
echo.| date | find "Current" > tmp$$$.bat
echo set weekday_=%%3> current.bat
call tmp$$$
echo %weekday_%
if "%weekday_%"=="Fri" echo Thank God it's Friday
if exist tmp$$$.bat del tmp$$$.bat
if exist current.bat del current.bat
set weekday_=
In fact, if you substitute %%4 for the %%3 in the above, you'll
capture today's date. Neat, eh?