How To Use Compound Commands in Windows Command Shell


At Windows command shell cmd.exe you could do something powerful with the compound commands, which I believe copies from *nix. Today Someone asked me how to write a script – executes the second command after the correct execution of the first command. He suspected that the IF isn’t the right choice. In fact, using a combination of command (Compound Command) is better.

There are three compound commands methods, which are single &, double & and || (double or).

Single & to run several tasks at the same time

Usage: The first command & second command [& The third command …]
This method can be performed simultaneously with multiple commands, regardless of whether the command is executed successfully or not.

C:\>dir z: & dir c:\helloacm
The system cannot find the path specified.
 Volume in drive C has no label.
 Volume Serial Number is 0078-59FB
 Directory of c:\helloacm
2002-05-14  23:51       <DIR>          .
2002-05-14  23:51       <DIR>          ..
2002-05-14  23:51                   14 sometips.gif

Double & (&&)

Usage: The first command && second command [&& third command …]
In this way you can perform multiple commands at the same time, when there is error in the command execution, the next commands are not executed, if there has been no error, it will always execute all the commands; Think of the boolean evaluation in programming concept: if one condition is known to be false, then the rest of the boolean parts will be skipped because the overall result will be false as well. (nearly all clever compilers optimise this).

C:\>dir z: && dir c:\helloacm
The system cannot find the path specified.
C:\>dir c:\helloacm && dir z:
 Volume in drive C has no label.
 Volume Serial Number is 0078-59FB
 Directory of c:\helloacm
2002-05-14  23:55       <DIR>         .
2002-05-14  23:55       <DIR>          ..
2002-05-14  23:55                   14 sometips.gif
               1 File(s)             14 bytes
               2 Dir(s)     768,671,744 bytes free
The system cannot find the path specified.

When doing backups you might use this command. For example:

dir \\192.168.0.1\database\backup.mdb && copy \\192.168.0.1\database\backup.mdb E:\backup 

If the file backup.mdb exists on a remote server, execute the copy command, otherwise the copy will be skipped. This can be replaced by using if exist command.

|| (Double OR)

Usage: The first command || second command [|| third command …]
This method can be performed simultaneously with multiple commands. If any of the command is executed successfully, then the next commands will be skipped. If all commands return error, then all commands will be executed. Think of boolean evaluation as well, then any part is known to be true (successful execution), then there is no need to execute the rest.

C:\helloacm>dir sometips.gif || del sometips.gif
 Volume in drive C has no label.
 Volume Serial Number is 0078-59FB
 Directory of C:\helloacm
2002-05-14  23:55                   14 sometips.gif
               1 File(s)             14 bytes
               0 Dir(s)     768,696,320 bytes free

The *nix shells are similar.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
574 words
Last Post: Tutorial: Create a Sample DLL Project using CodeBlocks IDE in C/C++
Next Post: Tutorial Example: How to Access Microsoft Excel (Chart) using the Windows Scripting Host (JScript)

The Permanent URL is: How To Use Compound Commands in Windows Command Shell

Leave a Reply