Batch Script - Bitwise Operators


Advertisements

The following code snippet shows how the various operators can be used.

Example

@echo off
SET /A "Result = 48 & 23"
echo %Result%
SET /A "Result = 16 | 16"
echo %Result%
SET /A "Result = 31 ^ 15"
echo %Result%

Output

The above command produces the following output.

16
16
16

Redirection

Redirection is a concept of taking the output of a command and re-directing that output to a different output media. The following commands are available for re-direction.

  • command > filename − Redirect command output to a file.

  • command >> filename − APPEND into a file.

  • command < filename − Type a text file and pass the text to command.

  • command 2> file − Write standard error of command to file (OS/2 and NT).

  • command 2>> file − Append standard error of command to file (OS/2 and NT).

  • commandA | commandB − Redirect standard output of commandA to standard input of command.

The following code snippet shows how the various redirection operations can be used.

command > filename

This command redirects command output to a file.

Example

@echo off 
ipconfig>C:\details.txt

Output

The output of the above program would be that all the details of the ipconfig command will be sent to the file C:\details.txt. If you open the above file, you might see the information similar to the one as the following.

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

This command appends the output of the command into a file.

Example

@echo off
systeminfo>>C:\details.txt

Output

The output of the above program would be that all the details of the systeminfo command will be appended to the file C:\details.txt. if you open the above file you might see the information similar to the one as the following.

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

This command types a text file and passes the text to command.

Example

@echo off
SORT < Example.txt

Output

If you define a file called Example.txt which has the following data.

4
3
2
1

The output of the above program would be

1
2
3
4

command 2> file

This command writes the standard error of command to file (OS/2 and NT).

Example

DIR C:\ >List_of_C.txt 2>errorlog.txt

In the above example, if there is any error in processing the command of the directory listing of C, then it will be sent to the log file errorlog.txt.

command 2>> file

Appends the standard error of command to file (OS/2 and NT).

Example

DIR C:\ >List_of_C.txt 2>errorlog.txt
DIR D:\ >List_of_C.txt 2>>errorlog.txt

In the above example, if there is any error in processing the command of the directory listing of D, then it will be appended to the log file errorlog.txt.

commandA | commandB

This command redirects standard output of commandA to standard input of command.

Example

Echo y | del *.txt

Output

The above command will pass the option of ‘y’ which is the value of ‘Yes’ to the command of del. This will cause the deletion of all files with the extension of txt.

batch_script_operators.htm
Advertisements