Return
Some More Tips and Tricks
Page(s) - Printed From? www.PCXT-Micro.com

Testing an environment variable value

If you use in a batch file something like 'if %1 == OK ?', this may crash your batch file because, if %1 is empty, it will result in a syntax error. Instead, use 'if %1. == OK. ?'.


PATH is limited to 128 characters

No, COMMAND.COM only is limited to a command-line of 128 characters long. This means that you can have a PATH up to 1000 characters if you want; the limitation is that you cannot type it on a command-line (even in a batch file). If you use?XSET? for example (a program that allows you to put EVERYTHING you want in a DOS environment variable and use it as if you had assigned it the value with the standard DOS command SET), you can extend your path (or any other environment variable) as you want.


The 'REM' command

COMMAND.COM reads and executes batch files one line at a time; that means that it reads one line, execute it and rereads the file from the beginning to the next line. If you do not have a good disk-cache installed, it is not efficient.

When using REM in your batch files to insert a remark, COMMAND.COM reads the comment line, execute it (i.e., does nothing) and rereads the file from the beginning to the next line.

Furthermore, if you put the REM command on the begin of a line containing a redirection ex: rem echo something > file.dat,? it will not execute the command after the REM, but will redirect nothing to the file output .

To avoid this, there is a trick: use '::' instead of 'REM'. ':' is understood as a label to be used by the 'GOTO' statement (see your?DOS?documentation); this line will never be executed. As a label cannot begin with a ':', this line will not be considered as an executable line, nor as a label. ex: replace "REM This batch file uses characters like >,..."?(sans quotes)?by?":: This batch file uses characters like >,..."?(sans quotes).


Display an empty line in a batch file

To echo an empty line on the screen, use ECHO:

Test if a directory exists

To test for the existence of a directory, test for the existence of the file nul.ext in it. Warning:?The filename "nul" may be sufficient on a standard FAT system, but not on a Novell network, a NTFS partition,... ex: if exist c:\mydir\nul.ext echo The directory 'MYDIR' exists.


"Chdir %var%" error message

If an environment variable contains a directory name ending with a '\', COMMAND.COM will not accept to chdir to this directory. ex: set dir=c:\mydir\ cd %mydir% ==> Error message: 'Invalid directory' The trick (to avoid removing the trailing '\') is to add a '.' ex: cd %mydir%. (note the trailing '.')


How to use special variables like 'windir'

Although it is possible to use some tricks to test it (like piping the SET command to FIND), it is almost impossible to actually use with plain DOS these special variables used by Windows because they are lowercase and COMMAND.COM does not allow you to use them. The easiest way to access them (even modify them) is to use?XSET? .


Enhanced batch file redirection

How to ease the reading of your batch files when using redirections. You maybe already discovered that the flow of your batch file sometimes becomes a bit unreadable when using a lot of redirections to files. Ex: echo This is my batch file echo: echo This is first line of first file > myfile1.out echo I will list all files to file 2 dir > myfile2.out echo This should be the 2nd line of first file >> myfile1.out attrib *.* >> myfile2.out.? Are you able to easily see where all commands are redirected to ? Here is another way of writing the same batch file which (amazingly) also works: echo This is my batch file echo: > myfile1.out echo This is first line of first file echo I will list all files to file 2 > myfile2.out dir >> myfile1.out echo This should be the 2nd line of first file >> myfile2.out attrib *.*.? Isn't that easier ?