批处理脚本 - 位运算符



以下代码片段显示了如何使用各种运算符。

示例

@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 的文件。

batch_script_operators.htm
广告
© . All rights reserved.