
- 批處理指令碼教程
- 批處理指令碼 - 首頁
- 批處理指令碼 - 概述
- 批處理指令碼 - 環境
- 批處理指令碼 - 命令
- 批處理指令碼 - 檔案
- 批處理指令碼 - 語法
- 批處理指令碼 - 變數
- 批處理指令碼 - 註釋
- 批處理指令碼 - 字串
- 批處理指令碼 - 陣列
- 批處理指令碼 - 決策
- 批處理指令碼 - 運算子
- 批處理指令碼 - 日期和時間
- 批處理指令碼 - 輸入/輸出
- 批處理指令碼 - 返回程式碼
- 批處理指令碼 - 函式
- 批處理指令碼 - 程序
- 批處理指令碼 - 別名
- 批處理指令碼 - 裝置
- 批處理指令碼 - 登錄檔
- 批處理指令碼 - 網路
- 批處理指令碼 - 列印
- 批處理指令碼 - 除錯
- 批處理指令碼 - 日誌記錄
- 批處理指令碼資源
- 批處理指令碼 - 快速指南
- 批處理指令碼 - 有用資源
- 批處理指令碼 - 討論
批處理指令碼 - 位運算子
以下程式碼片段顯示瞭如何使用各種運算子。
示例
@echo off SET /A "Result = 48 & 23" echo %Result% SET /A "Result = 16 | 16" echo %Result% SET /A "Result = 31 ^ 15" echo %Result%
輸出
以上命令產生以下輸出。
16 16 16
重定向
重定向是指獲取命令的輸出並將該輸出重定向到不同的輸出介質的概念。以下是可用於重定向的命令。
command > filename − 將命令輸出重定向到檔案。
command >> filename − 附加到檔案。
command < filename − 鍵入文字檔案並將文字傳遞給命令。
command 2> file − 將命令的標準錯誤寫入檔案 (OS/2 和 NT)。
command 2>> file − 將命令的標準錯誤附加到檔案 (OS/2 和 NT)。
commandA | commandB − 將 commandA 的標準輸出重定向到 commandB 的標準輸入。
以下程式碼片段顯示瞭如何使用各種重定向操作。
command > filename
此命令將命令輸出重定向到檔案。
示例
@echo off ipconfig>C:\details.txt
輸出
上述程式的輸出是 ipconfig 命令的所有詳細資訊將被髮送到檔案 C:\details.txt。如果您開啟上述檔案,您可能會看到類似於以下資訊。
Windows IP Configuration Wireless LAN adapter Local Area Connection* 11: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Ethernet adapter Ethernet: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Wireless LAN adapter Wi-Fi: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Tunnel adapter Teredo Tunneling Pseudo-Interface: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . :
command >> filename
此命令將命令的輸出附加到檔案。
示例
@echo off systeminfo>>C:\details.txt
輸出
上述程式的輸出是 systeminfo 命令的所有詳細資訊將被附加到檔案 C:\details.txt。如果您開啟上述檔案,您可能會看到類似於以下資訊。
Windows IP Configuration Wireless LAN adapter Local Area Connection* 11: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Ethernet adapter Ethernet: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Wireless LAN adapter Wi-Fi: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Tunnel adapter Teredo Tunneling Pseudo-Interface: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Host Name: WIN-50GP30FGO75 OS Name: Microsoft Windows Server 2012 R2 Standard OS Version: 6.3.9600 N/A Build 9600 OS Manufacturer: Microsoft Corporation OS Configuration: Standalone Server OS Build Type: Multiprocessor Free Registered Owner: Windows User Registered Organization: Product ID: 00252-70000-00000-AA535 Original Install Date: 12/13/2015, 12:10:16 AM System Boot Time: 12/30/2015, 5:52:11 AM System Manufacturer: LENOVO System Model: 20287 System Type: x64-based PC
command < filename
此命令鍵入文字檔案並將文字傳遞給命令。
示例
@echo off SORT < Example.txt
輸出
如果您定義了一個名為 Example.txt 的檔案,其中包含以下資料。
4 3 2 1
上述程式的輸出將是
1 2 3 4
command 2> file
此命令將命令的標準錯誤寫入檔案 (OS/2 和 NT)。
示例
DIR C:\ >List_of_C.txt 2>errorlog.txt
在上面的示例中,如果處理 C 目錄列表的命令時有任何錯誤,則它將被髮送到日誌檔案 errorlog.txt。
command 2>> file
將命令的標準錯誤附加到檔案 (OS/2 和 NT)。
示例
DIR C:\ >List_of_C.txt 2>errorlog.txt DIR D:\ >List_of_C.txt 2>>errorlog.txt
在上面的示例中,如果處理 D 目錄列表的命令時有任何錯誤,則它將被附加到日誌檔案 errorlog.txt。
commandA | commandB
此命令將 commandA 的標準輸出重定向到 commandB 的標準輸入。
示例
Echo y | del *.txt
輸出
上述命令將傳遞 'y'('Yes' 的值)選項到 del 命令。這將導致刪除所有副檔名為 txt 的檔案。