English 中文(简体)
Batch Script - Functions
  • 时间:2024-12-22

Batch Script - Functions


Previous Page Next Page  

A function is a set of statements organized together to perform a specific task. In batch scripts, a similar approach is adopted to group logical statements together to form a function.

As pke any other languages, functions in Batch Script follows the same procedure −

    Function Declaration − It tells the compiler about a function s name, return type, and parameters.

    Function Definition − It provides the actual body of the function.

Function Definition

In Batch Script, a function is defined by using the label statement. When a function is newly defined, it may take one or several values as input parameters to the function, process the functions in the main body, and pass back the values to the functions as output return types .

Every function has a function name, which describes the task that the function performs. To use a function, you "call" that function with its name and pass its input values (known as arguments) that matches the types of the function s parameters.

Following is the syntax of a simple function.

:function_name 
Do_something 
EXIT /B 0

    The function_name is the name given to the function which should have some meaning to match what the function actually does.

    The EXIT statement is used to ensure that the function exits properly.

Following is an example of a simple function.

Example

:Display 
SET /A index=2 
echo The value of index is %index% 
EXIT /B 0
S.No Functions & Description
1 Calpng a Function

A function is called in Batch Script by using the call command.

2 Functions with Parameters

Functions can work with parameters by simply passing them when a call is made to the function.

3 Functions with Return Values

Functions can work with return values by simply passing variables names

4 Local Variables in Functions

Local variables in functions can be used to avoid name confpcts and keep variable changes local to the function.

5 Recursive Functions

The abipty to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller.

6 File I/O

In Batch Script, it is possible to perform the normal file I/O operations that would be expected in any programming language.

7 Creating Files

The creation of a new file is done with the help of the redirection filter >. This filter can be used to redirect any output to a file.

8 Writing to Files

Content writing to files is also done with the help of the redirection filter >. This filter can be used to redirect any output to a file.

9 Appending to Files

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file.

10 Reading from Files

Reading of files in a batch script is done via using the FOR loop command to go through each pne which is defined in the file that needs to be read.

11 Deleting Files

For deleting files, Batch Script provides the DEL command.

12 Renaming Files

For renaming files, Batch Script provides the REN or RENAME command.

13 Moving Files

For moving files, Batch Script provides the MOVE command.

14 Batch Files – Pipes

The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command.

15 Batch Files – Inputs

When a batch file is run, it gives you the option to pass in command pne parameters which can then be read within the program for further processing.

16 Using the SHIFT Operator

One of the pmitations of command pne arguments is that it can accept only arguments till %9. Let’s take an example of this pmitation.

17 Folders

In Batch Script, it is possible to perform the normal folder based operations that would be expected in any programming language.

18 Creating Folders

The creation of a folder is done with the assistance of the MD (Make directory) command.

19 Listing Folder Contents

The psting of folder contents can be done with the dir command. This command allows you to see the available files and directories in the current directory.

20 Deleting Folders

For deleting folders, Batch Scripting provides the DEL command.

21 Renaming Folders

For renaming folders, Batch Script provides the REN or RENAME command.

22 Moving Folders

For moving folders, Batch Script provides the MOVE command.

Advertisements