批处理 中的 Batch Files – Pipes函数

首页 / 批处理入门教程 / 批处理 中的 Batch Files – Pipes函数

管道运算符(|)获取一个命令的输出(默认为STDOUT),并将其定向到另一命令的输入(默认为STDIN),如,以下命令对目录C:\的内容进行排序

dir C:\| sort

在此示例中,两个命令同时启动,但随后sort命令暂停,直到收到dir命令的输出, sort命令使用dir命令的输出作为输入,然后将其输出发送到句柄1(即STDOUT)。

以下是pipe命令的另一个示例。在此示例中,文件C:\new.txt的内容通过管道过滤器发送到sort命令。

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

命令&重定向组合

通常,在处理管道命令时,管道操作符与重定向操作符一起使用可提供有用的功能。

如,以下命令将首先获取C:\中定义的所有文件,然后使用pipe命令查找所有扩展名为.txt的文件。然后它将获取此输出并将其打印到文件AllText.txt。

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

多个管道命令

要在同一命令中使用多个过滤器,请用管道(|)分隔过滤器。如,以下命令搜索驱动器C:上的每个目录,查找包含字符串" Log"的文件名,然后一次在一个"命令提示符"窗口中显示它们-

链接:https://www.learnfk.comhttps://www.learnfk.com/batch-script/batch-script-files-pipes.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

Nginx核心知识150讲 -〔陶辉〕

Swift核心技术与实战 -〔张杰〕

现代C++编程实战 -〔吴咏炜〕

成为AI产品经理 -〔刘海丰〕

Python自动化办公实战课 -〔尹会生〕

如何落地业务建模 -〔徐昊〕

给程序员的写作课 -〔高磊〕

徐昊 · AI 时代的软件工程 -〔徐昊〕

好记忆不如烂笔头。留下您的足迹吧 :)