1) Comments
A batch comment is something that has absolutely no effect on the code, it is commonly used as a note or reference to the coder. In batch, :: is used to denotate a comment. A comment can be used after a command or on its own line, never before a command.
::This is a comment Pause ::This is also a comment |
The pause command allows the command prompt to pause the execution of the script, this allows the user to read and follow the programme. We will get into the pause command in more detail later.
2) echo and @echo
The echo command is used to print a line of text or numbers on the screen. For those familiar to C languages, it is similar to printf or cout >>
Syntax:
Code:
echo Hello world! pause ::Please note that this will pause the execution of the command ::Allowing the user to see the output |
Output:
C:\users\%username%:echo Hello world! Hello world! C:\users\%username%:pause Press any key to continue... |
Not very welcoming, is it? this is where the @echo off command comes in!
Code:
@echo off echo Hello world! pause |
Output:
Hello world! Press any key to continue... |
@echo on has the opposite effect:
@echo on turns user input on rather than off.
Code:
@echo off echo hi @echo on echo hi pause |
Output:
hi C:\users\%username%:echo hi hi C:\users\%username%:pause Press any key to continue... |
3) The pause command
Now for the final part of the lesson, we will learn the pause command. This command stops or pauses the programme from executing the whole application at once. Using the pause command will freeze the output until the user reacts.
Syntax:
@echo off pause |
Output:
Press any key to continue... |
Q: Is there a way of pausing the command without displaying the line of text 'Press any key to continue...'?
A: Yes! Just use this syntax instead.
Syntax:
pause >nul |
The >nul command voids the output of the console.
Now lets put everything we learned together to create the hello world programme!
Code:
@echo off echo Hello world! pause >nul |
Output:
Hello world! |