8/22/2021

If Noti Batchfiles

Introduction

Batch files are used to automate groups of commands which would ordinarily be executed from the command line.

Batch files are not so crucial nowadays, although still supported by newer versions of Windows. Under Windows XP/2000+, batch files (.bat) run in a special window (aka Command Prompt) created by c: window system32 cmd.exe (this could be called command.com in some instances). Conditional Processing with If. One of the most important capabilities of any programming language is the ability to choose from among different instructions based on conditions the program finds as it runs.

Benefits of using batch files include:

  • Savings in time and effort
  • Simplification of operations for inexperienced users
  • Simplification of complex commands for experienced users
  • Elimination of operator error

Common uses for batch files include:

  • Copying or deleting files
  • Creating the proper environment for an application
  • Setting environmental variables

A common practice is to place all batch files in one directory and then PATH to that directory. This centralization of batch files makes modifications easier if they become necessary.

Batch files are plain text files created using a plain text editor such as Notepad or the DOS EDIT text editor.

Each DOS command, and/or other external command, is placed on its own line along with all the required parameters and switches.

Batch files are executed by the command interpreter, Command.com. Command.com opens a batch file, reads the command on the first line, closes the batch file, executes the first command, and then repeats those three steps for each line in the batch file. Swat 4 the stetchkov syndicate original exergen.

This rather odd method of execution can be demonstrated with the following batch file:

Commands Specific to Batch Files

Echo

Echo is a feature of batch files which repeats (echos) each line of the batch file to the screen as it is executed.

Although useful for debugging purposes, once production is completed ECHO serves no purpose other than creating a messy screen which may confuse unsophisticated users.

To 'clean up' the batch file operation, turn off ECHO by placing the ECHO OFF command on the first line of the batch file. Since the command does not take effect until after it is executed, DOS will echo that first line. To suppress the echo of that first line, place '@' in front of it. The '@' command can also be used to selectively repress echo as required.

The ECHO command also can be used to display text within a batch file to the screen. Text on a line in a batch file which is preceded by ECHO will appear on the screen regardless of whether or not the ECHO OFF command has been issued.

Rem

The command processor will ignore any line in a batch file which is preceded by the Rem command. The Rem command (Rem-ark) is frequently used at the beginning of lines which are intended to document a batch file's operation. It can also be used to disable a selected line or lines for debugging purposes.

Call

When a program (.com or .exe) is run from within a batch file, control returns to the batch file when the program is terminated. But when a command in a batch file is the name of another batch file, control does not return to the first batch. Rather, when the second batch file has terminated it will return to the command line.

Here is an illustrated of this. As written, the last line of the first of the first batch file below will never execute:

To make control return to the first batch file, insert the CALL command in front of the command to execute Testtwo.bat.After Testtwo.bat terminates, control returns to Testone.bat and execution picks up where it left off. This time the last line WILL be executed.

Batch File Examples

The following batch file examples can be pasted into a text editor andmodified to match your environment. If you paste them into a word processor, be sure that you Save As a plain text file.

Displaying a text file

When large amounts of text are to be displayed to the screen from within a batch file, a more convenient alternative to ECHOing text to the screen is to place the text in a separate file and then display the contents of that file:Or, when there is more text that can fit on one screen:

Batch

The second example takes advantage of redirection, see below.

Batch files if not exist

Deleting Temporary Files

Place the following lines in an AUTOEXEC.BAT to clean out temporary files during the boot. (Note the use of redirection, covered in detail below).

Control a Printer

Noti

Use a one-line batch file like this one to issue a form feed to a HP laser printer:

Find a Disk File

A batch file like the following one can be used to find a disk file from the command line.

The /v switch for CHKDSK is for 'Verbose', which willlist all files on the specified disk. That output is piped ( ) through the FIND filter.

The replaceable parameters %1 and %2 stand in for the drivedesignation and the filename being searched for and make the batch file moreflexible. (See replaceable parameters below for more information about them).

Advanced Batch File Techniques

Redirection

All DOS commands have some output. For example, the output of the DIRcommand is a directory listing, while the output of the COPY command is '1 file(s) Copied'.

The default destination for all command output is the device named CON -the console, or monitor. Command output can, however, be redirected to otherDOS devices such as PRN, NUL, or a disk file. The signs of redirection are > and <.

Redirection works because DOS treats all devices as file handles. Study the examples in the table below:
Redirected CommandResult
DIR > PRN Printout of the directory listing on the default printer
DIR > dir.txt A directory listing written to the file DIR.TXT
(If the file DIR.TXT already exists, it is overwritten)
DIR >> dir.txt A directory listing written to the file DIR.TXT
(If the file DIR.TXT already exists, the directory listing is appended to the end of the file)
CHKDSK >> sysinfo.txt The output of CHKDSK is written to the file SYS.INFO.TXT
If the file already exists the output is appended to the end of the file
COPY filename > NUL The output of COPY (1 file(s) Copied) is sent to the NUL device.
The NUL device, otherwise known as the 'bit bucket', is a test device which is a virtual dead-end. In other words, the output gets sent nowhere.
This technique is commonly used to hide batch file screen output which cannot otherwise be suppressed with ECHO OFF.

Redirection can also be used to respond to DOS commands. For example,if we put a command to delete all files at some location (DEL [path]*.*) in a batch file,the delete command will pause the batch file execution to ask 'Are You Sure?'.

To avoid this we can redirect the contents of a file containing 'Y' (yes)and a carriage return (Enter) as input to the DEL command:

First, in the batch file folder, create a text file named YES containing the letter 'y' followed by a carriage return. Then, in a batch file use a line like:

Replaceable parameters

Replaceable parameters pass additional input provided by the user fromthe command line to a specified place within a batch file. This makes it possible to write more flexible batch files.

Create a batch file such as this:

Execute the batch file this way:

And the batch file will switch to the dbase folder and execute dbase.exe.

Execute the batch file this way:

Noti

And the batch file will switch to the myeditor folder and execute ed.exe.

IF and GOTO

If Noti Batchfiles

Bat File To Run Exe

The combination of the IF statement and the GOTO statement can be used to create branching logic within a batch file.

Batch If Not Exist Two Files

For example, if a batch file is designed to function with a replaceable parameter itis desirable to make a test to see whether or not the user actually enteredthe parameter at the command line and bail out if none was given.

Study the following example:

In this example, IF testing and GOTO <:LABEL> is used to prevent the accidental formatting of C:

LOOP

The LOOP command permits the creation of batch files that repeat:

Concatenation

CommandResult
COPY FILE1 + FILE2 FILE3Combines files 1 and 2 in file 3
COPY FILE1 + FILE2Appends file 2 to end of file 1
COPY FILE + Update date and time of file
COPY FILE + /bAs above for a .COM file (/b ignores Ctrl Z)