%1 and %* - Receive Parameters in Batch File

Q

How to receive parameters in batch file?

✍: FYIcenter.com

A

If a batch file is invoked with parameters, you receive them in special variables:

  • %0 - The file name of this batch file.
  • %1, %2, %3, ... - The first, second, third, ..., parameters provided when this batch file was invoked.
  • %* - All parameters in a single string.

Here is batch file called, Batch-File-Parameters.bat, that displays those special variables holding batch file parameters:

@echo off

echo (%0)
echo (%1) (%2) (%3) (%4) (%5)
echo (%*)

if you run the above batch file with different parameters, you get:

C:\fyicenter>Batch-File-Parameters.bat 1 2 3 4 5
(Batch-File-Parameters.bat)
(1) (2) (3) (4) (5)
(1 2 3 4 5)

C:\fyicenter>Batch-File-Parameters.bat 1+2 3+4-5
(Batch-File-Parameters.bat)
(1+2) (3+4-5) () () ()
(1+2 3+4-5)

C:\fyicenter>Batch-File-Parameters.bat "1 2" 3 "4 5"
(Batch-File-Parameters.bat)
("1 2") (3) ("4 5") () ()
("1 2" 3 "4 5")

 

Introduction of Windows Batch File

⇒⇒Windows Batch File Tutorials

2017-12-05, 117👍, 0💬