批處理檔運行時,可以選擇傳遞命令行參數,然後在程式中讀取這些參數以進一步處理。 批處理檔參數可以從程式中使用%
運算符以及參數的數字位置進行調用。 以下是如何定義命令行參數。
%0
是被調用的程式名稱。%1
是第一個命令行參數。%2
是第二個命令行參數。- 所以直到
%9
。
我們來看一個如何使用命令行參數的簡單例子。
示例
@echo off
echo The first parameter is %1
echo The second parameter is %2
echo The third parameter is %3
如果上面的代碼存儲在一個名為test.bat
的檔中,並且該檔運行為 -
test.bat 5 10 15
那麼,以下將是輸出 -
The first parameter is 5
The second parameter is 10
The third parameter is 15