]> Essential Windows commands tips – part 3 « MOleYArd (MOYA) blog

About

This blog mostly presents different approaches, methods and practices in software and web development, but also contains some "out of main topic" articles. MOYA (MOleYArd) products are presented here as well.

Follow

 


Valid XHTML 1.0 Transitional

Essential Windows commands tips – part 3

At the end of the previous part of this tutorial we briefly introduced echo command and I promised that we will deal with creating batch files in the command prompt. Actually, creating batch files is the second step. The first step is to create an arbitrary file.

First let’s talk about echo again. Type

echo /?

to see how to use echo and what is its purpose. The output is:

Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

Last time we showed turns command-echoing on or off usage. This time ECHO [message] is the important one. Try few examples:

C:\Users\moya>echo text
text

C:\Users\moya>echo car
car

Now we are almost done – we can echo text to the command line. Only other thing to do is to redirect that output to a file. Change current directory to Desktop:

cd Desktop

and type:

echo car > ourfile.txt

In general command > filename redirect command output to a file. That’s exactly what we needed. Now ourfile.txt contains car string. Type

echo car > ourfile.txt

to see it. The file ourfile.txt will be opened and will contain car string.

Now type

echo house > ourfile.txt

and open the file again. It now contains house string and car string was rewritten. What about not rewriting original content, but just appending it? It’s easy:

echo car >> ourfile.txt

This will append echoed content to the end of the file. Its first line will now contain house and its second line car.

Now try to echo some longer sentence:

echo This is a tutorial about essential Windows commands. > ourfile.txt

What if we would like to add several sentences (in separate line each). We can do this with several commands using append (>>). But we can also use one compound command. Try this:

echo This is a tutorial about essential Windows commands. > ourfile.txt && echo This is the second sentence. >> ourfile.txt

We joined two commands with &&. Actually, we don’t have to open ourfile.txt in the external text editor. We can check output in command line as well with type command. Let’s type

type ourfile.txt

and we will see the content of ourfile.txt file.

Now we can probably imagine how to create batch files this way. It’s absolutely the same as the creation of any other file. We can see that this way of creating files is not at all practical, but it is one of the ways. But I guess you will rather use some text editor (though possibly command line based) for these kinds of tasks. We introduced this – &&. This stuff is much more practical. It serves for chaining commands. There are more types of chainings:

  • command1 & command2 Run command1 and then command2
  • command1 && command2 Run command1 and if commandA succeeds then run command2
  • command1 || command2 Run command1 and if it fails then run command2

You can use these when you write your own batch files or if you use more complicated compound commands. We now know something about redirection of an output. Besides printing output applications (and thus their commands) might also have some input. An output of one application (command) may serve as an input for another one. We have actually encountered this when we used command tree (tree | more). But we haven’t yet really explained what it exactly does.

  • command1 | command1 Pipe the output from command1 into command2

It pipes output. It exactly means that the output of the command is served as the input for the another command. Utility more takes output of another command and then print it line-by-line or page-by-page when user press Enter or Space. So that’s all for this part :) .

Comments are closed.