Batch Script - Files Pipes


Advertisements

The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. For example, the following command sorts the contents of the directory C:\

dir C:\ | sort

In this example, both commands start simultaneously, but then the sort command pauses until it receives the dir command's output. The sort command uses the dir command's output as its input, and then sends its output to handle 1 (that is, STDOUT).

Following is another example of the pipe command. In this example, the contents of the file C:\new.txt are sent to the sort command through the pipe filter.

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

Combining Commands with Redirection Operators

Usually, the pipe operator is used along with the redirection operator to provide useful functionality when it comes to working with pipe commands.

For example, the below command will first take all the files defined in C:\, then using the pipe command, will find all the files with the .txt extension. It will then take this output and print it to the file AllText.txt.

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

Using Multiple Pipe Commands

To use more than one filter in the same command, separate the filters with a pipe (|). For example, the following command searches every directory on drive C:, finds the file names that include the string "Log", and then displays them in one Command Prompt window at a time −

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

Following are some examples of how the pipe filter can be used.

Examples

The following example send’s the list of all running tasks using the tasklist command and sends the output to the find command. The find command will then find all processes which are of the type notepad and display them in the command prompt.

tasklist | find "notepad"

Output

Following is a sample output.

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

The following example send’s the list of all running tasks using the tasklist command and sends the output to the more command. The more command will then display the lists of running tasks one page at a time.

Example

tasklist | more

Output

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 --

The following example send’s the list of all running tasks using the tasklist command and sends the output to the find command. The find command will then find all processes which are of the type notepad and then uses the redirection command to send the content to the file tasklist.txt.

Example

tasklist | find "notepad" > tasklist.txt

Output

If you open the file tasklist.txt, you will get the following sample output.

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
Advertisements