How to add paremeter on a batch file

To make a batch file reusable and flexible you may want to make it able to accept parameters. For example, you create a batch file that will compile and run a java application. What if you want to compile another java file with a different file name, this is were parameterized batch file comes in.

Below is a sample code, just copy paste it to Notepad and save as `hello.bat`

@echo off
cd C:\Documents and Settings\guest\Desktop
javac %1.java
java -cp . %1


How to use:

On command prompt, type: hello HelloWorld

Note:
1. `hello` is the batch file
2. `HelloWorld` is the first parameter which stands for %1
3. You may use many parameters: %1 %2 %3 %4

How it works:

1. It changes directory where the file is
2. It compiles a java file - HelloWorld (first parameter) + .java
3. It runs HelloWorld compiled class

No comments: