Batch files

What are batch files?

A batch file is a file that contains a list of DOS commands that are usually executed in order, and the format of such a file is .bat.

Batch files

To begin with, let's start with the ECHOECHO command. ECHO is a very useful command, and is used to display a message predefined by the programmer.

ECHO [ON | OFF]
ECHO [message]

where:

  • ON - Activates ECHO, i.e. displays the current directory and commands executed from a batch file are visible.
  • OFF - Disables ECHO, that is, it hides the current directory and only shows the result (i.e. the output) of the commands executed.
  • message - Displays the specified message.

We will use ECHO OFF in this case to disable ECHO.

CLSCLS is used to clear the screen and PAUSEPAUSE is used to pause the program execution, which will resume as soon as the user presses a key.

As we just saw, we can also use ECHO to compose a message.

Now let's take a very simple example, which summarizes the various instructions a bit:

echo off
cls
echo Ciao fratello!
pause

This way it will display Ciao fratello! (Hello brother! in italian) as a message, and thanks to the PAUSE command it can be viewed by the user if the command is not run from the command prompt, otherwise the window would close before the user has time to read. This will be the exact result:

Batch file message example

If you don't want to clear the screen when the batch file starts but still want to hide the echo of the first line, you may write @ECHO OFF.

You can also use ECHO to leave a blank line. However, by just typing ECHO, it will only show you whether ECHO è has been activated or not. However, there is a well-known workaround, which is to put a non-alphanumeric character after ECHO, for example:

echo.

This trick can also be used to write messages without the command being interpreted differently, for example:

echo.off

This way, instead of disabling the command echo, it will print off on the screen.

To ask the user a question, you use the CHOICE, but you must take into account that it is not compatible with all versions of Windows. In this case, you can use an almost equivalent syntax, which however has some different functionality compared to CHOICE. Also note that CHOICE is a hidden commandhidden command.

CHOICE /C:letters [/N] [/T:time,letter] [text]
CHOICE /C letters [/N] [/CS] [/T time /D letter] [/M text]

where:

  • /C:letters - Specifies the letters the user can choose to reply.
  • /N - Hides the letters that the user can choose.
  • /T:time,letter - Specifies the waiting time in seconds, after which the specified letter will be automatically chosen.
  • text - Specifies the text that will be displayed, for example a question.
  • /CS - Enables case sensitivity, i.e. it distinguishes between upper case and lower case.

Example:

echo off
cls
choice /c:ynt /n /t:20,t Delete the file C:\file.txt? [Y/N]
echo.
if errorlevel 3 (
    echo Time expired. The file was not deleted.
) else if errorlevel 2 (
    echo The file will not be deleted.
) else if errorlevel 1 (
    del "C:\file.txt"
    echo File deleted.
)
echo.
pause

Here it asks whether to delete the file C:\file.txt. The available letters are Y, N and T, but they are not displayed; T will be chosen after 20 seconds have passed. If T is chosen it will display Time expired. The file was not deleted., if N is chosen it will display The file will not be deleted. and if Y is chosen it will delete C:\file.txt and display File deleted..

In the example the construct IF ERRORLEVEL n is used , where n is a number that will be recorded upon response; if the chosen letter is the first, 1 will be recorded, if the second, 2, etc… (see IFIF).

Since IF ERRORLEVEL n tells us whether the value of ERRORLEVEL is greater than or equal to n, we must start the condition at the highest possible value. For example:

if errorlevel 2 (echo b) else if errorlevel 1 (echo a)

and not:

if errorlevel 1 (echo a) else if errorlevel 2 (echo b)

In the latter case it would always execute the command ECHO a.

In some versions of Windows, the only way to prompt the user for input is to use SET /P. Let's take an example similar to the previous one:

echo off
cls
:start
set /p a=Delete the file C:\file.txt? [Y/N]
echo.
if /i "%a%"=="Y" (
    del "C:\file.txt"
    echo File deleted.
    goto end
)
if /i "%a%"=="N" (echo The file will not be deleted.) else (
    echo Choose between Y and N.
    set a=
    echo.
    goto start
)
:end
echo.
pause

In place of a you can specify any variable name, other than PATH, EXTPATH etc… and in place of start you can put any word.

Here we have introduced a new command: GOTOGOTO. It is used to direct the command processor to a label, for example GOTO LOL takes me to the label :LOL. If GOTO :EOF is executed, the batch file execution is terminated (EOF stands in fact for End of File).

The time limit cannot be entered with the SETSET command, but it can take more than one character as input.

Example:

echo off
cls
:start
set /p a=Insert the password to continue.
echo.
if "%a%"=="Ajeje" goto ok
echo.
goto start
:ok
echo Welcome to Pinco Pallino's new program!
echo.
echo ...
echo.
pause

The REMREM command is used to insert comments, which will be ignored by the command processor.

CALLCALL is used to open a batch file in another window; it is used with the following syntax:

CALL {[drive:][path]batchfile | :label}

where:

  • drive:][path]batchfile - Opens the specified batch file in a new window.
  • :label - Sends the command processor to the specified label (similar to GOTO label).

So:

goto abc

is similar to:

call :abc

Writing to file

There are two syntax types: one overwrites the entire contents of the file with the output of the specified command; the other appends the output of the specified command to the end of the file. If the file does not exist, it will be created in both cases.

command > file

command >> file

The first syntax overwrites the entire contents of the file with the command output. The second syntax appends the command output to the end of the file, without overwriting anything.

Example:

echo off
cls
echo Fool who reads. > file.txt
echo IHIHIH >> file.txt

If you use nul instead of the file name, the output will not be copied anywhere. Its real utility is to be able to hide the output of a command.

Example:

echo off
cls
echo Press a key to terminate the application.
pause > nul

If you have any questions, feel free to ask them in the Comments section below.

Comments