批處理指令碼 - 檔案輸入



在執行批處理檔案時,可以選擇傳遞可以在程式內部讀取以便進一步處理的命令列引數。批處理檔案的引數可以使用 % 運算子及引數的相應數字位置在程式內部呼叫。以下是命令列引數的定義方式。

  • %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
batch_script_functions.htm
廣告
© . All rights reserved.