批處理指令碼 - 檔案管道



管道運算子(|)接收一個命令的輸出(預設情況下為 STDOUT),並將其定向到另一個命令的輸入(預設情況下為 STDIN)。例如,以下命令對 C:\ 目錄的內容進行排序。

dir C:\ | sort

在這個例子中,兩個命令同時啟動,但隨後 sort 命令暫停,直到它接收 dir 命令的輸出。sort 命令使用 dir 命令的輸出作為其輸入,然後將其輸出傳送到控制代碼 1(即 STDOUT)。

以下是管道命令的另一個示例。在這個示例中,檔案 C:\new.txt 的內容透過管道過濾器傳送到 sort 命令。

@echo off 
TYPE C:\new.txt | sort

結合命令與重定向運算子

通常,管道運算子與重定向運算子一起使用,以便在處理管道命令時提供有用的功能。

例如,以下命令將首先獲取 C:\ 中定義的所有檔案,然後使用管道命令查詢所有副檔名為 .txt 的檔案。然後它將獲取此輸出並將其列印到檔案 AllText.txt 中。

dir C:\ | find "txt" > AllText.txt

使用多個管道命令

要在同一個命令中使用多個過濾器,請使用管道符號(|)分隔這些過濾器。例如,以下命令搜尋驅動器 C: 上的每個目錄,查詢包含字串“Log”的檔名,然後一次在一個命令提示符視窗中顯示它們 -

dir c:\ /s /b | find "TXT" | more

以下是一些管道過濾器使用方法的示例。

示例

以下示例使用 tasklist 命令傳送所有正在執行的任務列表,並將輸出傳送到 find 命令。然後,find 命令將查詢所有型別為記事本的程序並在命令提示符中顯示它們。

tasklist | find "notepad"

輸出

以下是一個示例輸出。

notepad.exe               1400 Console            1      8,916 K
notepad.exe               4016 Console            1      11,200 K
notepad.exe               1508 Console            1      8,720 K
notepad.exe               4076 Console            1      8,688 K

以下示例使用 tasklist 命令傳送所有正在執行的任務列表,並將輸出傳送到 more 命令。然後,more 命令將一次顯示一頁正在執行的任務列表。

示例

tasklist | more

輸出

Image Name                PID Session Name  Session#     Mem Usage
======================    ================  ===========  ============
System Idle Process           0 Services        0             4 K
System                        4 Services        0           276 K
smss.exe                    344 Services        0         1,060 K
csrss.exe                   524 Services        0         4,188 K
csrss.exe                   608 Console         1        58,080 K
wininit.exe                 616 Services        0         3,528 K
winlogon.exe                644 Console         1         5,636 K
services.exe                708 Services        0         7,072 K
lsass.exe                   716 Services        0        10,228 K
svchost.exe                 784 Services        0        10,208 K
svchost.exe                 828 Services        0         7,872 K
dwm.exe                     912 Console         1       208,316 K
nvvsvc.exe                  932 Services        0         6,772 K
nvxdsync.exe                968 Console         1        16,584 K
nvvsvc.exe                  976 Console         1        12,780 K
svchost.exe                1008 Services        0        20,340 K
svchost.exe                 224 Services        0        39,740 K
svchost.exe                 468 Services        0        11,864 K
svchost.exe                 860 Services        0        11,184 K
svchost.exe                 232 Services        0        16,992 K
wlanext.exe                1168 Services        0        12,840 K
-- More --

以下示例使用 tasklist 命令傳送所有正在執行的任務列表,並將輸出傳送到 find 命令。然後,find 命令將查詢所有型別為記事本的程序,然後使用重定向命令將內容傳送到檔案 tasklist.txt。

示例

tasklist | find "notepad" > tasklist.txt

輸出

如果開啟檔案 tasklist.txt,您將獲得以下示例輸出。

notepad.exe            1400 Console            1      8,916 K
notepad.exe            4016 Console            1      11,200 K
notepad.exe            1508 Console            1      8,720 K
notepad.exe            4076 Console            1      8,688 K
batch_script_functions.htm
廣告