- Batch Script - Logging
- Batch Script - Debugging
- Batch Script - Printing
- Batch Script - Network
- Batch Script - Registry
- Batch Script - Devices
- Batch Script - Aliases
- Batch Script - Process
- Batch Script - Functions
- Batch Script - Return Code
- Batch Script - Input / Output
- Batch Script - DATE & TIME
- Batch Script - Operators
- Batch Script - Decision Making
- Batch Script - Arrays
- Batch Script - Strings
- Batch Script - Comments
- Batch Script - Variables
- Batch Script - Syntax
- Batch Script - Files
- Batch Script - Commands
- Batch Script - Environment
- Batch Script - Overview
- Batch Script - Home
Batch Script Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Batch Script - Functions
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 | A function is called in Batch Script by using the call command. |
2 | Functions can work with parameters by simply passing them when a call is made to the function. |
3 | Functions can work with return values by simply passing variables names |
4 | Local variables in functions can be used to avoid name confpcts and keep variable changes local to the function. |
5 | The abipty to completely encapsulate the body of a function by keeping variable changes local to the function and invisible to the caller. |
6 | In Batch Script, it is possible to perform the normal file I/O operations that would be expected in any programming language. |
7 | 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 | 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 | 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 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 | For deleting files, Batch Script provides the DEL command. |
12 | For renaming files, Batch Script provides the REN or RENAME command. |
13 | For moving files, Batch Script provides the MOVE command. |
14 | 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 | 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 | 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 | In Batch Script, it is possible to perform the normal folder based operations that would be expected in any programming language. |
18 | The creation of a folder is done with the assistance of the MD (Make directory) command. |
19 | 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 | For deleting folders, Batch Scripting provides the DEL command. |
21 | For renaming folders, Batch Script provides the REN or RENAME command. |
22 | For moving folders, Batch Script provides the MOVE command. |