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 ECHO command. ECHO is a very useful command and commonly used to display a message predefined by the programmer.

 

ECHO [ON | OFF]

ECHO [message]

 

ON      Enable ECHO, that is, the current folder and the commands executed by batch

            files are visible.

OFF    Disable ECHO, i.e. hide the current folder and executed commands,

            showing only the result (i.e. the output).

message    Display the specified message.

 

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

 

CLS is used to clear the screen and PAUSE is used to pause the program execution, which will resume as soon as he 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, otherwise the window would close before the user has the time to read. This will be the exact result:

 

 

It is also possible to use ECHO to leave a blank line. However, by writing just ECHO we will only be shown via a message whether ECHO has been activated or not. Luckily there is a well-known workaround, which is to put a non-alphanumeric character after ECHO, for example:

 

echo.

 

 

To ask a question to the user, you use the CHOICE, but you have to take into account that it is not compatible with all versions of Windows. In such cases, you can also use an somewhat equivalent syntax, which however has some subtle different features compared to CHOICE.

 

CHOICE /C:letters [/N] [/T:time,letter] [testo]

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

 

/C:letters    Specify the letters that the user can choose to reply.

/N                Hide the letters the user can choose.

/T:time,letter    Specify the waiting time in seconds; when the time is up the specified letter will

                                be automatically chosen.

text            Specify the text that will be displayed, for example a question.

/CS              Enable case sensitivity, that is, distinguish between uppercase and lowercase.

 

Example:

 

echo off

cls

choice /c:ynt /n /t:20,t Delete C:\file.txt? [Y/N]

echo.

if errorlevel 3 (

echo Timeout. File 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 Timeout. File 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...

Since IF ERRORLEVEL n tells us whether the value of ERRORLEVEL is greater than or equal to n, we must starrt 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 ECHO a command.

 

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 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

 

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

 

Here is a new command: GOTO. 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 would in fact stand for End of File).

 

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

Example:

 

echo off

cls

:start

set /p a=Enter password to continue. 

echo.

if "%a%"=="Ajeje" goto ok

echo.

goto start

:ok

echo Welcome to the new Pinco Pallino program!

echo.

echo ...

echo.

pause

 

The REM command is used to insert commands.

 

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

 

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

 

[drive:][path]batchfile    Open the specified batch file in a new window.

:label                         Send 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 one 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 when it is not desired.

 

Example:

 

echo off

cls

echo Press any key to terminate the application.

pause > nul

If you have any questions, please feel free to post them in the Comments section.