How To Pass Parameter In Windows Batch File

Passing parameters in a Windows batch file is a common and useful technique that allows you to customize your scripts with user input.

This tutorial will show you how to pass parameters in your batch files, giving you more flexibility and control over your script.

Steps:

1. Define your parameter(s)

Before you can pass a parameter in your batch file, you first need to define what that parameter is. This can be done using the % symbol followed by a name that you choose. For example, to define a parameter called “filename”, you would use the following code:

Here, we are using the “set” command to define a variable called “filename” and assign it the value of %1 (which means the first parameter passed to the batch file).

2. Use your parameter(s) in your script

Once your parameter(s) have been defined, you can use them in your script as you would any other variable. For example, if you wanted to display the filename passed to the batch file, you would use the following code:

echo The file you selected is %filename%

Here, we are using the “echo” command to display a message that includes the value of the “filename” parameter.

3. Pass parameters when calling your batch file

To pass parameters when calling your batch file, you simply need to include the parameter(s) after the file name. For example, if your batch file is called “myscript.bat” and you want to pass the filename “example.txt” as a parameter, you would use the following command:

This will call your batch file and pass the “example.txt” parameter to it. You can include multiple parameters by separating them with spaces.

Conclusion:

Passing parameters in a Windows batch file is a powerful technique that can give you more control over your scripts. By following the steps outlined in this tutorial, you can define, use, and pass parameters in your batch files with ease.

Here is an example of a batch file that uses parameters to copy a file:

To use this batch file, you would call it with two parameters: the source file and the destination directory. For example, to copy “file.txt” to the “C:\temp” directory, you would use the following command:

Output:

Copying example.txt to C:\temp...
        1 file(s) copied.