How to create a Batch file (.bat or .cmd)

How to create a Batch file bat or cmd

Do you want to automate actions under Windows? Create a batch file, with a .bat or .cmd extension: you can then chain several commands automatically each time you launch it.

In many practical sheets published on Comment Ça Marche, we invite you to launch the cmd.exe function, the Windows command line tool (and more specifically DOS, still present in the Microsoft operating system). The lines make it possible to perform actions in the system without resorting to a specific program or software with a graphical interface. A Batch file, which has the extension .bat or .cmd, allows you to combine one or more commands that will be executed sequentially, ie, one after the other. The whole thus constitutes a script which will chain the commands indicated. This can be, for example, backing up a particular folder, shutting down or restarting the PC, or even opening a website. The applications are numerous and relatively simple, as long as you know the commands. Creating a Batch file does not require any special software. Any text editor – starting with Notepad provided in Windows – can create it since this file only contains text. However, it is advisable to proscribe word processors – such as Word, Wordpad, Write, etc.-– which add “slag”, and to favor editors producing “pure” text, without hidden code. Then simply save this text file with the .bat or .cmd extension to make it a Batch file. Say then, just open the file – by double clicking on its icon, for example – to launch the Batch and run the script it contains. Practice !

How to create a .bat or .cmd file in Windows?

Creating a Batch file presents no difficulty. Here, for example, is how to create a Batch file to automatically launch the default Web browser so that it displays the commentcamarche.net site while presenting the weather forecast for the next 24 hours in an additional command window.

► Right-click on the Windows Desktop. From the menu that appears, choose New > Text document.

► A new file has appeared on the Desktop named New Text Document. Right-click on it and choose Open with > Notepad.

35584588

► Windows Notepad opens with a blank document. Now enter the command lines that you want to save in your batch file, making sure to validate each of them by pressing the key Hall on the keyboard to make a line break. For our example, we specify this:

start https://commentcamarche.net

finger [email protected]

pause

We indicate here that we want to open the home page of commentcamarche.net in the browser and display the 24-hour weather forecast for the city of Versailles.

35584590

► Pull down the menu File and choose Save as.

35584613

► Pull down the menu Kind at the bottom of the window and choose All the files.

35584623

► Finally give a name to your Batch file and finish by associating the file extension .cmd Where .bat (both work the same way). Validate with a click on To register.

35584624

► Your Batch file is ready. It features an icon flanked by two toothed wheels. Double click on it.

35584626

► Immediately the commands are executed. The Web browser opens on the Commentcamarche.net site while next to it, a Windows command window presents the weather forecast for the city indicated.

35584629

Of course, the example described above is – intentionally – very simple and not very useful. It is just intended to show you the procedure for creating and using a Batch file, which remains the same, regardless of the length and complexity of its contents. You can create an infinity of Batch files according to your needs, to automate all kinds of tasks – there are also very many of them on the Internet and in forums. You just need to know the DOS commands and their syntax (see our fact sheet on the Windows command prompt, which lists the main DOS commands). In practice, your imagination is almost the only limit to the possibilities of a Batch file – with DOS commands, of course. You can create as many Batch files as you want, especially since they “weigh” nothing – they are pure text.

ccn1