1 Create a shortcut to that batch file 2 Right click on that shortcut file and choose Properties 3 Click the Advanced button to find a checkbox for running as administrator I did Steps 1 and 2. Fun Things to Do With Batch Files: ok script kiddies and pros alike, its time for my third instructable. Before i start, i wanna tell you guys something. I want to hear what YOU guys would like to see next. If you have a fun idea for a batch file, email it to gcolonna@sympatico.ca w.

  1. Batch File For Do
  2. Batch File To Delete File
  3. My Batch Files Batch Files Linux
  4. Batch File To Open File
  5. My Batch Files Batch Files Download
  6. My Batch Files Batch Files File

Introduction

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

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.

Batchfiles

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.

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.

My Batch Files Batchfiles

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.

Batch File For Do

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:

Call batch file from batch file

The second example takes advantage of redirection, see below.

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).

Batch File To Delete File

Control a Printer

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

My Batch Files Batch Files Linux

Redirection

My Batch Files Batchfiles

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:

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

IF and GOTO

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

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)

Batch File To Open File

In this tutorial, you will learn about batch file functions and how functions are written in batch file programs.

Batch script functions: Introduction
Function definition
Function call
Basic function example
Function with parameters
Function with return values

Batch file functions: Introduction

A function is a single comprehensive unit (self-contained block) containing a block of code that performs a specific task. The function performs the same task when called which avoids the need of rewriting the same code again and again.

My Batch Files Batch Files Download

Like in any other programming language, batch file functions also have:

  • function definition
  • function call

NOTE: When defining the main program is to ensure that the statement EXIT /B %ERRORLEVEL% is used in the main program to separate the code of the main program from the function.

Batch File Function Definition

My Batch Files Batchfiles

-Syntax

As shown in syntax, a function definition in batch file starts with the declaration of a function with a label. Next is the body of function where we write codes to achieve a certain task. At the end EXIT /B 0 is to ensure successful termination or proper exit of the functions.

Batch File Functions Call

Like in every other programming language, to use the defined functions, we need to call it in the main program. For this, CALL command is used.

-Syntax

So, this is how the function is called in batch files.

In the case of batch file functions with parameters, a tilde(~) sign is used to access them in the main program along with the positional number of parameter.

Similarly, in the case of batch file functions with return values, return values are set in the function using the set command and the tilde(~) character along with the positional number of the return values.

Here is the batch file program to demonstrate the use of the basic function, function with parameters and function with return values.

Batch file function example: Program with basic function

Output

Note that the EXIT /B %ERRORLEVEL% is used to separate the function from the main program and PAUSE is used to hold the screen, else output console will vanish within nanoseconds.

My Batch Files Batch Files File

Batch file function example: Program demonstrating function with parameters

Output

Here tilde(~) sign is used to access the parameter’s value followed by the position of the parameter. Hence, ~1 is for accessing first parameter and ~2 for the second.

Batch file function example: Program demonstrating function with return values

Output

Here SET command along with tilde(~) sign is used to set the return values depending on the position followed by tilde sign.