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:
or you can do as following:
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 “&”:
we can use “&&”:
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:
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:
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.