If you are working on a batch file script first time and you require to pass some value to a batch file at run time rather than hardcoding them into the file, then you will think it would not be easy. But believe me, it is easy very easy to pass parameter in batch file script.
In this article, I will explain how to pass parameters in the batch file script by command line arguments.
So let’s assume a scenario where you need to pass emp-id and employe name from the command line argument in a batch script. But before solving this problem I want to introduce with batch parameter.
Batch parameters (Command line parameters):
In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on.
If you require all arguments, then you can simply use %* in a batch script. %*refers to all the arguments (e.g. %1 %2 %3 %4 %5 …) but only arguments %1 to %9 can be referenced by number.
Note: %0 is a special case, as this contains the name of the batch file itself.
Let’s see an example to understand how to pass parameters in the batch script. In this example, I am passing two-parameter emp-id and employee names in the batch script by a command-line argument. In the batch script, I am printing the passed parameters on the console using the echo.
@echo off REM: print new line echo. echo ----------------------------------------- echo *** Test Script for parameter *** echo ----------------------------------------- REM: print new line echo. echo EMP-ID is %1 REM: print new line echo. echo Employee Name is %2
While running this script output will be:
Parameter extension:
When an argument is used to supply a filename then the following extended syntax can be applied. Here we are using the variable %1 as an example.
Batch Parameter | Description |
---|---|
%~1 | Expands %1 and removes surrounding quotation marks. |
%~f1 | Expands %1 to a fully qualified path. |
%~d1 | Expands %1 to a drive letter only. |
%~p1 | Expands %1 to a path only. |
%~n1 | Expands %1 to a file name only. |
%~x1 | Expands %1 to a file name extension only. |
%~s1 | Expands %1 to a fully qualified path that contains short names only. |
%~a1 | Expands %1 to the file attributes. |
%~t1 | Expands %1 to the date and time of the file. |
%~z1 | Expands %1 to the size of the file. |
%~$PATH:1 | Searches the directories listed in the PATH environment variable, and expands %1 to the fully qualified name of the first directory found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string. |
The modifiers can combine with the batch parameters for compound results:
Batch Parameter with Modifier | Description |
---|---|
%~dp1 | Expands %1 to a drive letter and path only. |
%~nx1 | Expands %1 to a file name and extension only. |
%~dp$PATH:1 | Searches the directories listed in the PATH environment variable for %1, and then expands to the drive letter and path of the first directory found. |
%~ftza1 | Expands %1 to display output similar to the dir command. |
Recommended Articles for you:
- Batch file introduction.
- List of Batch script commands.
- How to create variables in the batch script.
- Batch script to copy files from one folder to another folder.
- How to use if-else statements in the batch script.
- for loop in the batch file.