Old good command line #5

This time the post will be not about the commands, but about some helpful tricks while dealing with them. Actually these features are the very basics of cmd, still many people don’t know about them. These ones are related to running several commands in one line with various conditions.

&”. For example you’d like to restart a service. What you can do in PowerShell is simply

Get-Service w32time | Restart-Service

In cmd you are to start two commands:

image

or you can do as following:

image

that will do the same, but faster.

&&”. Going a little bit further we can think of not running the second command if the first has not been successful. For example, if we failed to create a folder, we won’t be able to copy files to it anyway, so why bother to? That is instead of “&”:

image

we can use “&&”:

image
You see, there is only one “access denied”, that is the file copy was not even tried.

||”. These are for running the next command only in case the first one failed. I succeeded in making up such a situation:

image

What happened here is that first copy failed and after that we create the directory and copy file again. In case the directory exists the picture will be like the following:

image

The file just got copied and that’s all.

The last </irony mode>“piece of wisdom” </irony mode off>: you can even group your commands so that to be executed in the same order you want them to be run.

Previous issues: #0, #1, #2, #3, #4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s